网页上文件上传功能发生的错误。如:我们用file field html 控件
设置它的enctype为multipart
再放置一个listbox用于显示上传的文件列表
放置按钮,在它的click事件中:
private void butUpLoad_Click(object sender, System.EventArgs e)
{
string strFileName=filUpLoad.PostedFile.FileName;//运行是显示这里错误
strFileName=System.IO.Path.GetFileName(strFileName);
try
{
if(filUpLoad.PostedFile.ContentLength==0)
throw new System.IO.FileNotFoundException(); filUpLoad.PostedFile.SaveAs(Request.MapPath(Request.ApplicationPath)+"\\"+strFileName);
lstServerFiles.Items.Add(strFileName);
lstServerFiles.SelectedIndex=0;
}
catch(System.NullReferenceException ex)
{
Response.Write("未将对象引用到实例");
}

catch(System.IO.FileNotFoundException ex)
{
Response.Write("<p>no thi file </p>"+ex.Message+"<br>");
}
catch(System.IO.IOException ex)
{
Response.Write("<p>thi file can not be saved"+"to the server.</p>"); }
catch(System.Net.WebException ex)
{
Response.Write("<p>an internet error occurred while "+"uploading the file.</p>"); }
catch(Exception ex)
{
Response.Write("<p>the following error occured :"+ex.Message+"<br>");
} finally
{
//Response.Write("error occurred");
filUpLoad.Value="";
}
}下面是相关错误信息:“/UpLoadTest”应用程序中的服务器错误。
--------------------------------------------------------------------------------未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 50:  private void butUpLoad_Click(object sender, System.EventArgs e)
行 51:  {
行 52:  string strFileName=filUpLoad.PostedFile.FileName;
行 53:  strFileName=System.IO.Path.GetFileName(strFileName);
行 54:  try
 源文件: c:\inetpub\wwwroot\uploadtest\webform1.aspx.cs    行: 52 堆栈跟踪: 
[NullReferenceException: 未将对象引用设置到对象的实例。]
   UpLoadTest.WebForm1.butUpLoad_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\uploadtest\webform1.aspx.cs:52
   System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   System.Web.UI.Page.ProcessRequestMain() 大家看看什么问题

解决方案 »

  1.   

    检查一下你的服务器控件是不是runat="server"的
    并确定你上传了文件
      

  2.   

    是runat="server"就是点击上传时出错误了我简单修改下
    <form id="Form1" method="post" enctype="multipart/form-data" runat="server">
    将click事件异常处理的finally改成:
    finally
    {
    Response.Write("error occurred");

    }这样可以运行,不过提示相应文件拒绝访问
      

  3.   

    上传到Web服务器,用户必须要有相应的权限如果是Win2000,请添加ASPNET用户的写权限如果是Winxp or win2003 请添加NetWork Service用户的写权限具体操作鼠标右键你要上传得文件夹 -- 属性 -- 安全 -- 添加 输入相应的用户名  确定 -- 给与相应的权限 -- 确定
      

  4.   

    先看看你用来保存上传文件的目录是否用足够的权限
    需要添加 Internet来宾用户 并赋予其 修改权限