0

I have a form with a multiple fileupload on. (You can select multiple files within the same filecontrol

These files I have to upload to an API

If I don't have a multiple, but a single fileupload, I can do

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

If have multiple fileupload, I can use this to iterate over the files:

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;

          }
    }

But I don't have uploadfile.FileBytes

How can I get the contents of the file to a string?

Leif Neland
  • 1,416
  • 1
  • 17
  • 40
  • Possible duplicate of [How to create byte array from HttpPostedFile](https://stackoverflow.com/questions/359894/how-to-create-byte-array-from-httppostedfile) – VDWWD Jun 30 '17 at 13:16

1 Answers1

0

I got it to work like this:

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());
    ...
    }
}
Leif Neland
  • 1,416
  • 1
  • 17
  • 40