0

我有一个 AsyncFileUpload 控件:

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

在其OnUploadedComplete事件中,我正在编写此代码:

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

}

现在我必须阅读上传文件的内容......我有一个功能:

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";
}

现在我的查询是如何让上传的文件传递给这个函数?

更新

我已经这样做了:

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);

}

但出现错误...找不到文件

4

1 回答 1

1

我不确定我是否理解了这个问题,但这并不容易:

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将文件保存在服务器上,这样你就可以用它做你想做的事。

于 2012-11-19T13:14:59.467 回答