2016-02-25 51 views
-3

我有一個列表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); 
    } 
} 
+2

串什麼代碼行是否發生錯誤?設置一個斷點並找出答案。 – Seano666

+0

當我使用列表來分配它與VAR它顯示這是錯誤 –

+0

當你通過代碼時,什麼是文件的值也看起來像你應該使用for循環,而不是foreach看看看起來是什麼類似於你正在嘗試做的http://stackoverflow.com/questions/29510007/c-sharp-getting-all-files-in-request-file – MethodMan

回答

2

這是很可疑:foreach (string s in Request.Files)Request.Files[s]; 我猜你不希望指數「Request.Files」(無論是)從「Request.Files」本身

+1

這評論,而不是答案。 –

+0

這無疑與問題有關。也許OP可以告訴我們他正在用Request.Files [s] – Seano666

+0

@EricJ做什麼。我試圖用問題中提供的信息指出OP的正確方向。在這種情況下,我不可能提供完整的答案,因爲「這樣做會解決它」,所以我發佈了這個答案。很抱歉,如果這是一個錯誤,我還是個菜鳥SO,所以任何建議都是歡迎的。 –