索引超出范围。必须为非负值并小于集合大小。参数名: index ????????

chenyulancn 2006-05-11 08:04:20
上传图片按钮
private void Button1_Click(object sender, System.EventArgs e)
{

HttpFileCollection MyFileColl = HttpContext.Current.Request.Files;
HttpPostedFile MyPostedFile = MyFileColl[1]; // 提示此句出错

if (MyPostedFile.ContentType.ToString().ToLower().IndexOf("image") < 0)
{
Response.Write("无效的图形格式。");
return;
}
.............

索引超出范围。必须为非负值并小于集合大小。参数名: index
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。参数名: index

源错误:


行 194:
行 195: HttpFileCollection MyFileColl = HttpContext.Current.Request.Files;
行 196: HttpPostedFile MyPostedFile = MyFileColl[1];
行 197:
行 198: if (MyPostedFile.ContentType.ToString().ToLower().IndexOf("image") < 0)


源文件: c:\inetpub\wwwroot\sheji\shangchuan.aspx.cs 行: 196

堆栈跟踪:


[ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
参数名: index]
System.Collections.ArrayList.get_Item(Int32 index) +91
System.Collections.Specialized.NameObjectCollectionBase.BaseGet(Int32 index) +10
System.Web.HttpFileCollection.Get(Int32 index)
System.Web.HttpFileCollection.get_Item(Int32 index)
sheji.shangchuan.Button1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\sheji\shangchuan.aspx.cs:196
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()


...全文
708 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiaolei189 2006-05-11
  • 打赏
  • 举报
回复
把html中下面这句话改一下!<form id="Form1" method="post" encType="multipart/form-data" runat="server"> 在中间加上encType="multipart/form-data"就可以了!
UDOBE 2006-05-11
  • 打赏
  • 举报
回复
<INPUT TYPE="FILE" ID="FILE1" RUNAT="SERVER">

注意 RUNAT="SERVER"
chenyulancn 2006-05-11
  • 打赏
  • 举报
回复
多谢各位!!!
我已改成
HttpPostedFile MyPostedFile = MyFileColl[0];
但还是提示相同的错误!! 我的是( vs 2003)

to xu8512(中国土匪)
我觉的你说的对,请问怎么改呢?? 谢谢!!!!

再次求救!! 谢谢了!!!!
xu8512 2006-05-11
  • 打赏
  • 举报
回复
MyFileColl监视下它的值,可能没有取到值,所以才造成超出索引
studydotnet 2006-05-11
  • 打赏
  • 举报
回复
大家都说了,楼主这样做还是错??
xiaohutushen 2006-05-11
  • 打赏
  • 举报
回复
HttpFileCollection 上传文件数组,下标是从0开始的.
如果你只上传一个图片的话,就应该是MyFileColl[0];
webwait 2006-05-11
  • 打赏
  • 举报
回复
行 196: HttpPostedFile MyPostedFile = MyFileColl[1];
==
HttpPostedFile MyPostedFile = MyFileColl[0];
Eallies 2006-05-11
  • 打赏
  • 举报
回复
HttpPostedFile MyPostedFile = MyFileColl[0]; // 提示此句出错
我这里改为0时测试正确。测试环境:vs2005
smile9961 2006-05-11
  • 打赏
  • 举报
回复
HttpPostedFile MyPostedFile = MyFileColl[1]; // 提示此句出错
-->
HttpPostedFile MyPostedFile = MyFileColl[0];

chenyulancn 2006-05-11
  • 打赏
  • 举报
回复
上传图片按钮
private void Button1_Click(object sender, System.EventArgs e)
{

HttpFileCollection MyFileColl = HttpContext.Current.Request.Files;
HttpPostedFile MyPostedFile = MyFileColl[0]; // 提示此句出错

if (MyPostedFile.ContentType.ToString().ToLower().IndexOf("image") < 0)
{
Response.Write("无效的图形格式。");
return;
}
.............

索引超出范围。必须为非负值并小于集合大小。参数名: index
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。参数名: index

源错误:


行 194:
行 195: HttpFileCollection MyFileColl = HttpContext.Current.Request.Files;
行 196: HttpPostedFile MyPostedFile = MyFileColl[1];
行 197:
行 198: if (MyPostedFile.ContentType.ToString().ToLower().IndexOf("image") < 0)


源文件: c:\inetpub\wwwroot\sheji\shangchuan.aspx.cs 行: 196

堆栈跟踪:


[ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
参数名: index]
System.Collections.ArrayList.get_Item(Int32 index) +91
System.Collections.Specialized.NameObjectCollectionBase.BaseGet(Int32 index) +10
System.Web.HttpFileCollection.Get(Int32 index)
System.Web.HttpFileCollection.get_Item(Int32 index)
sheji.shangchuan.Button1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\sheji\shangchuan.aspx.cs:196
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()



62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧