我有一個列表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