Question

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

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top