0

i have a AsyncFileUpload control :

<asp:AsyncFileUpload ID="venfileupld" runat="server" OnUploadedComplete="ProcessUpload" />

on its OnUploadedComplete event i am writing this code :

protected void ProcessUpload(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
    string name = venfileupld.FileName.ToString();
    string filepath = "upload_excel/" + name;
    venfileupld.SaveAs(Server.MapPath(name));

}

now i have to read the content of the uploaded file ... i have a function for that:

public void writetodb(string filename)
{
    string[] str;
    string vcode = "";
    string pswd = "";
    string vname = "";
    StreamReader sr = new StreamReader(filename);
    string line="";

    while ((line = sr.ReadLine()) != null)
    {
        str = line.Split(new char[] { ',' });
        vcode = str[0];
        pswd = str[1];
        vname = str[2];
        insertdataintosql(vcode, pswd, vname);
    }
    lblmsg4.Text = "Data Inserted Sucessfully";
}

now my query is that how i can get the uploaded file to pass to this function ?

UPDATE

i have done this :

protected void ProcessUpload(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
    string name = venfileupld.FileName.ToString();
    string filepath = "upload_excel/" + name;
    venfileupld.SaveAs(Server.MapPath(name));
    string filename = System.IO.Path.GetFileName(e.FileName);
    writetodb(filepath);

}

but getting an error... file not found

Arindam Das
  • 699
  • 4
  • 20
  • 39

1 Answers1

1

I'm not sure if i have understood the problem, but isn't it easy as:

protected void ProcessUpload(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
    string name = System.IO.Path.GetFileName(e.filename);
    string dir = Server.MapPath("upload_excel/");
    string path = Path.Combine(dir, name);
    venfileupld.SaveAs(path);
    writetodb(path);
}

SaveAs will save the file on the server, so you can do what you want with it afterwards.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • i am getting an error `The SaveAs method is configured to require a rooted path, and the path 'upload_excel/VENDOR.csv' is not rooted.` on line `venfileupld.SaveAs(path);` – Arindam Das Nov 19 '12 at 13:23
  • @ArindamDas: Edited my answer. The directory is `Server.MapPath("upload_excel/")` and the name is `System.IO.Path.GetFileName(e.filename)`. – Tim Schmelter Nov 19 '12 at 13:26
  • hello sir that solution u gave worked fine in my localhost but when i am doing this in a hosting server i am getting error.. the file is getting uploaded in the server but i think i am not getting the file for the function `writetodb()` can u 1sec help me on this ?\ – Arindam Das Nov 21 '12 at 05:43