日期:2014-05-18  浏览次数:20478 次

多个文件上传
<script language = "C#" runat = "server">
  public void UploadImg(string imgid)
  {
  HttpPostedFile img = Request.Files[imgid];
  if (img.ContentLength > 0)
  {
  string imgpath = "..\\web\\";
  string filename = img.FileName;
  img.SaveAs(Server.MapPath(imgpath) + filename);
  Response.Write(imgid + "上传成功");
  }
  else
  {
  Response.Write(imgid + "没有文件");
  }

  }
   
public void page_load(object sender , EventArgs E) 
{

int ImgCount = Request.Files.Count;
  int i;
  for (i = 0; i < ImgCount; i++)
  {
  string filetagname = "img" + (i+1);
  UploadImg(filetagname);
  }
}
</script>

出错提示:

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误: 


行 5: {
行 6: HttpPostedFile img = Request.Files[imgid];
行 7: if (img.ContentLength > 0) ------------>出错行
行 8: {
行 9: string imgpath = "..\\web\\";

这是什么问题啊?

------解决方案--------------------
Request.Files[imgid]; 
没找到这个东西
------解决方案--------------------
C# code

HttpFileCollection files = Request.Files;
for (int i = 0; i < files.Count; i++) {
  files[i].SaveAs(...);
}