0

I have a sample code from one of the plugin I was trying.

 HttpResponseMessage hrm = client.PostAsync(uri, content).GetAwaiter().GetResult();
 if (hrm.StatusCode == HttpStatusCode.OK)
 {
     MultipartMemoryStreamProvider provider = hrm.Content.ReadAsMultipartAsync().GetAwaiter().GetResult();
     if (provider.Contents.Count > 0)
     {
        for (int i = 0; i < provider.Contents.Count; i++)
        {
           HttpContent rcontent = provider.Contents[i];
           byte[] bytes = rcontent.ReadAsByteArrayAsync().GetAwaiter().GetResult();

           File.WriteAllBytes(@"c:\temp\ConvertToPDF" + i + ".pdf", bytes);
         }
      }
 }

This code posts n number of files to an api, and get the same files in pdf format as a response.

Then the response is saved into different files. Right now, MultipartMemoryStreamProvider has n number of files in the Contents collection. But, both of those files are actually pointing to the last file that I selected.

Code to create the request

 //Multipart content
 MultipartContent content = new MultipartContent();

 foreach (var singleFile in files)
 {
    //Create the StreamContent of the input file and add to the MultpartContent
    StreamContent is1 = new StreamContent(singleFile.InputStream);
    is1.Headers.ContentType = new MediaTypeHeaderValue(singleFile.ContentType);
    is1.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
    is1.Headers.ContentDisposition.Name = singleFile.FileName;
    is1.Headers.ContentDisposition.FileName = singleFile.FileName;
    content.Add(is1);
 }

This is the code that creates the content object. I checked the object and it actually contains two different files. So, I am unsure why this below line is causing an issue.

MultipartMemoryStreamProvider provider = hrm.Content.ReadAsMultipartAsync().GetAwaiter().GetResult();

P.S. Plugin name is ActivePDF DocConverter. Sadly it does not provide a hosted demo link, so I can't provide the url here. I am using my local machine to host that plugin.

Bukk94
  • 419
  • 1
  • 6
  • 23
jitendragarg
  • 945
  • 1
  • 14
  • 54
  • So the PDF creation works, but the files returned in you foreach are both the same document? How do you retrieve the files? – Greg Jul 17 '18 at 13:09
  • Actually, all I use is the byte array. They are different documents. Funnily, say I have abc.jpg and abc.png. They are both converted and the updated file name becomes abc.pdf for both of them. Funnily, that updated file name in the header is useless, as all I need is the byte array. – jitendragarg Jul 17 '18 at 13:17

1 Answers1

0

Okay, so I figured it out. Turns out that conversion tool is doing a replace on file name, when it returns the converted file. So, test.xls and test.doc are both converted properly, but new file name for both is test.pdf. Apparently the plugin has not considered the fact that file names can be same. So, by default, I decided that I will add a counter in the filename. Below is the updated code.

foreach (var singleFile in files)
{
   //Create the StreamContent of the input file and add to the MultpartContent
   StreamContent is1 = new StreamContent(singleFile.InputStream);
   is1.Headers.ContentType = new MediaTypeHeaderValue(singleFile.ContentType);
   is1.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
   is1.Headers.ContentDisposition.Name = singleFile.FileName;
   is1.Headers.ContentDisposition.FileName = x + singleFile.FileName;
   content.Add(is1);
   x++;
}

x is just an integer variable. I know it is dirty, but other than asking the plugin team to fix their code, this seems to be the only solution.

Bukk94
  • 419
  • 1
  • 6
  • 23
jitendragarg
  • 945
  • 1
  • 14
  • 54