读书人

多个文件上传,该怎么解决

发布时间: 2012-02-21 16:26:23 作者: rapoo

多个文件上传
<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(...);} 

读书人网 >asp.net

热点推荐