【发布时间】:2016-02-25 23:00:36
【问题描述】:
我有一个列表List<string> img = new List<string>(); 来存储列表中图像的每个路径,一旦文件上传它应该 img.Add(filename) 到列表中,但它显示错误 System.ArgumentOutOfRangeException这意味着列表中没有存储文件名并且列表为空。有人知道发生了什么吗?
foreach (string s in Request.Files)
{
HttpPostedFile file = Request.Files[s];
int fileSizeInBytes = file.ContentLength;
string fileName = file.FileName;// Request.Headers["X-File-Name"];
string fileExtension = "";
if (!string.IsNullOrEmpty(fileName))
{
fileExtension = Path.GetExtension(fileName);
string savedFileName = Guid.NewGuid().ToString() + fileExtension;
string path = HttpContext.Current.Server.MapPath("~/img/items/");
string filename = path + savedFileName;
file.SaveAs(filename);
img.Add(filename);
}
}
【问题讨论】:
-
错误发生在哪一行代码?设置断点并找出答案。
-
当我使用列表将其分配给 var 时,它表明这是错误
-
当您单步执行代码时,file 的值是什么,看起来您应该使用 for 循环而不是 foreach 看看看起来与您正在尝试做的事情相似的地方stackoverflow.com/questions/29510007/…
-
@MethodMan 上传多个文件工作正常问题是用文件名填充列表
-
所以在 if 语句中它在这里失败了
img.Add(filename);?如果img停在那条线上时的值是多少.. 你能显示完整的方法,以便我们可以看到List<string> img = new List<string>();的声明位置...确保它是在类级别声明的,使它成为公共静态的'可能为空,需要初始化.. 听起来像是 PostBack 问题
标签: c# asp.net c#-4.0 dropzone.js