我有一个列表List<string> img = new List<string>();
存储列表中的图像的每个路径一旦文件上传它应img.Add(文件名)的列表,但它显示错误System.ArgumentOutOfRangeException这意味着没有存储在列表中的文件名该列表是空的。有人知道发生了什么事?如何用foreach填充<>使用foreach?
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);
}
}
串什么代码行是否发生错误?设置一个断点并找出答案。 – Seano666
当我使用列表来分配它与VAR它显示这是错误 –
当你通过代码时,什么是文件的值也看起来像你应该使用for循环,而不是foreach看看看起来是什么类似于你正在尝试做的http://stackoverflow.com/questions/29510007/c-sharp-getting-all-files-in-request-file – MethodMan