运行孟子E章(http://dotnet.aspx.cc/ShowDetail.aspx?id=45E7E33C-F149-450E-B5D5-832958C20538
)提供的c#版本代码,以下是部分代码:========================================================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; 

GetThumbNail(MyPostedFile.FileName,100,100,MyPostedFile.ContentType.ToString(),false,MyPostedFile.InputStream);  }========================================================
以下是错误提示内容
“/create_file1”应用程序中的服务器错误。
--------------------------------------------------------------------索引超出范围。必须为非负值并小于集合大小。
参数名: index 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
参数名: index源错误: 
行 94:  {
行 95:  HttpFileCollection MyFileColl = HttpContext.Current.Request.Files; 
行 96:  HttpPostedFile MyPostedFile = MyFileColl[0];
行 97:  if (MyPostedFile.ContentType.ToString().ToLower().IndexOf("image") < 0) 
行 98:  { 
 源文件: c:\inetpub\wwwroot\create_file1\webform1.aspx.cs    行: 96 堆栈跟踪: 
[ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
参数名: index]
   System.Collections.ArrayList.get_Item(Int32 index) +2776397
   System.Collections.Specialized.NameObjectCollectionBase.BaseGet(Int32 index) +9
   System.Web.HttpFileCollection.Get(Int32 index) +5
   System.Web.HttpFileCollection.get_Item(Int32 index) +4
   create_file1.WebForm1.Button1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\create_file1\webform1.aspx.cs:96
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102
===========================================================
 提示索引超出范围,必须为非负值并小于集合大小,baidu,goole了一下也没找出想要的答案,哪位遇到过类似的问题,请帮忙指点,谢谢!

解决方案 »

  1.   

    HttpContext.Current.Request.Files
    这里面没有东西,
    执行这个HttpPostedFile MyPostedFile = MyFileColl[0];之前:
    if(MyFileColl.Count>0)
    {.......}
      

  2.   

    有没有设form的enctype="multipart/form-data"属性呢?
      

  3.   

    HttpFileCollection MyFileColl = HttpContext.Current.Request.Files;
    HttpPostedFile MyPostedFile = MyFileColl[0];
    ---------------------------------------------
    当MyFileColl对象为空或元素为0时,访问MyFileColl[0]就会报这样的错.
      

  4.   

    有没有设form的enctype="multipart/form-data"属性呢?
    文件上传框 有没有Name属性呢???
      

  5.   

    问题已经解决,在多问一个:oImg.Save(Server.MapPath("images") + "\" + strGuid + strFileExt, GetImageType(strContentType))
    对比孟子的代码后发现少了这段代码就不能把图片保存了,请问如何转换为c#格式的语法?