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

4
  • What's the problem, why can't you simply call that method from ProcessUpload? Commented Nov 19, 2012 at 13:13
  • You access the file that is now on the server itself. You know where the document is because of filepath in your very own code. Commented Nov 19, 2012 at 13:16
  • i called that but i am getting error .. file not found Commented Nov 19, 2012 at 13:17
  • Yes, because you're saving it as Server.MapPath(name) but you are passing System.IO.Path.GetFileName(e.FileName) to the method. Have you tried my code below? Commented Nov 19, 2012 at 13:23

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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);
@ArindamDas: Edited my answer. The directory is Server.MapPath("upload_excel/") and the name is System.IO.Path.GetFileName(e.filename).
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 ?\

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.