2017-06-30 28 views
0

我有一個多個文件上載的表單。 (您可以在同一filecontrol選擇多個文件多個文件上傳的文件字節

這些文件我要上傳到API

如果我沒有多,但單個文件上傳,我可以做

byte[] filedata = FileUploadControl.FileBytes; 
String filestring = Convert.ToBase64String(filedata); 

如果有多個文件上傳,我可以用它來遍歷文件:

HttpFileCollection fileCollection = Request.Files; 
for (int i = 0; i < fileCollection.Count; i++) 
    { 
     HttpPostedFile uploadfile = fileCollection[i]; 
     if (uploadfile.ContentLength > 0) 
     { 
      Int32 ContentLength = uploadfile.ContentLength; 
      String ContentType = uploadfile.ContentType; 
      string filename = uploadfile.FileName; 

      } 
    } 

但我沒有uploadfile.FileBytes

如何獲取文件的內容到字符串?

+0

的可能的複製[如何創建HttpPostedFile字節數組(https://stackoverflow.com/questions/359894/how-to-create-byte -array從 - httppostedfile) – VDWWD

回答

0

我得到了它這樣的工作:

HttpFileCollection fileCollection = Request.Files; 
for (int i = 0; i < fileCollection.Count; i++) 
{ 
    HttpPostedFile uploadfile = fileCollection[i]; 
    if (uploadfile.ContentLength > 0) 
    { 
    ... 
     MemoryStream ms = new MemoryStream(uploadfile.ContentLength); 
     uploadfile.InputStream.CopyTo(ms); 
     String filestring = Convert.ToBase64String(ms.ToArray()); 
    ... 
    } 
}