网页上文件上传功能发生的错误。如:我们用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() 大家看看什么问题
设置它的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() 大家看看什么问题
解决方案 »
- 关于SQL的COUNT(*)
- asp.net 前台c# script(不是JavaScript) 和后台代码的交互
- 用Microsoft Visual Studio 2005除了作网站,还能作什么,最好给出一个简单实用的小例子的代码
- 正则表达式提取内容的问题
- 请问过来人:如何将一个没有解决方案和项目文件的1.1的网站转换成2.0的网站?
- 帮忙看一个控件单击响应的问题,头疼!
- 运行ASP.NET的AJAX技术时总出错怎么办
- .net2.0 报错
- 急!!网页的发布的问题
- 大家帮忙看看引用dll的问题,不同的服务器一个通过一个不通过
- 怎样给CheckBoxList中的每一项设置特定的ID
- [求助]用户控件如何方便的验证?
并确定你上传了文件
<form id="Form1" method="post" enctype="multipart/form-data" runat="server">
将click事件异常处理的finally改成:
finally
{
Response.Write("error occurred");
}这样可以运行,不过提示相应文件拒绝访问
需要添加 Internet来宾用户 并赋予其 修改权限