2014-09-21 13 views
0

我正在實施通過asp.net fileupload控件&將上傳的文件保存到數據庫的多個文件上傳。在迭代文件時,我試圖設置File類的Data屬性。對於單個文件,我可以將其設置爲File.Data = postingFile.FileBytes。 但是,在迭代文件時,FileBytes屬性值每次都會變得相似。 任何人都可以解釋可能的原因嗎?我怎樣才能實現它?如何在asp.net中查找單個上傳文件的filebytes?

代碼示例:

if (fuAttachment.HasFiles) 
    { 
     foreach (HttpPostedFile postedFile in fuAttachment.PostedFiles) 
     { 
      DataAccess.File file = new DataAccess.File(); 
      file.Data = fuAttachment.FileBytes;//not working for multiple files. 
     } 
    } 
+0

不該」你是從'postedFile'讀取文件的內容嗎? – vcsjones 2014-09-21 15:47:01

回答

0

記住,文件上傳控制仍然張貼文件幕後。

這裏基本上是如何檢索多個文件,這可與一個和/或多個文件上傳控件,並計算ALL張貼的文件上傳控件的所有

HttpFileCollection uploadedFiles = Request.Files; 
for (int i = 0; i < uploadedFiles.Count; i++) 
{ 
    HttpPostedFile userPostedFile = uploadedFiles[i]; 
    try 
    { 
     if (userPostedFile.ContentLength > 0) 
     { 
      //do stuff with your file, check the attributes and functions of 'userPostedFile' to see what you can get from it, there is an input stream as well as file name among everything else. 
     } 
    } 
    catch (Exception Ex) 
    { 
     //here do stuff if file upload fails, adjust exception type as needed. 
    } 
}