如何使用fileupload Control将文件上传到aspx页面?

时间:2011-08-03 01:43:50

标签: c# asp.net

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/MasterPage.Master"
    CodeBehind="Mortgageclient.aspx.cs" Inherits="MortgageSaver._Default" %>

protected void btnfupload_Click(object sender, EventArgs e)
        {


            try
            {
                if (FileUpload1.HasFile)
                {
                    if (!Directory.Exists(Server.MapPath("Documents")))
                    {
                        Directory.CreateDirectory(MapPath("Documents"));

                    }
                    string docment = FileUpload1.PostedFile.FileName;
                    string path = System.IO.Path.GetFileName(docment);
                    FileUpload1.PostedFile.SaveAs(Server.MapPath("Documents/") + path);
                    lblstatus.ForeColor = System.Drawing.Color.Red;
                    lblstatus.Text = "Selected File has been Uploaded...";
                }
            }
            catch
            {
                lblstatus.ForeColor = System.Drawing.Color.Red;
                lblstatus.Text = "Selected File has not been Uploaded...";
            }
    }

这是我上传文件的代码,但它从对话框中选择一个文件,但是当我点击UploadFile按钮时,它显示“所选文件尚未上传。”如果我可以单独运行此页面,我正在工作,但我在上面的tabpanel中使用它,所以在这种情况下它不起作用。

1 个答案:

答案 0 :(得分:0)

FileUpload控件不能与异步回发一起使用。请参阅this页面,一直向下滚动到“在UpdatePanel控件内部使用Web部件控件”部分。替代解决方案是来自Ajax工具包的“AsyncFileUpload”。