Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将多部分内存流读入文件_C#_File_Multipart - Fatal编程技术网

C# 将多部分内存流读入文件

C# 将多部分内存流读入文件,c#,file,multipart,C#,File,Multipart,我有一个我尝试的插件的示例代码 HttpResponseMessage hrm = client.PostAsync(uri, content).GetAwaiter().GetResult(); if (hrm.StatusCode == HttpStatusCode.OK) { MultipartMemoryStreamProvider provider = hrm.Content.ReadAsMultipartAsync().GetAwaiter().GetResult()

我有一个我尝试的插件的示例代码

 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);
         }
      }
 }
这是创建内容对象的代码。我检查了对象,它实际上包含两个不同的文件。所以,我不确定为什么下面这一行会引起问题

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


另外,插件名为ActivePDF DocConverter。遗憾的是,它没有提供托管的演示链接,所以我不能在这里提供url。我正在使用我的本地计算机来托管该插件

好吧,我想出来了。结果表明,转换工具在返回转换后的文件时,正在对文件名进行替换。因此,test.xls和test.doc都被正确转换,但两者的新文件名都是test.pdf。显然,插件没有考虑到文件名可以相同的事实。 因此,默认情况下,我决定在文件名中添加一个计数器。下面是更新的代码

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
只是一个整数变量。我知道这是肮脏的,但除了要求插件团队修复他们的代码,这似乎是唯一的解决方案

所以PDF创建工作正常,但是foreach中返回的文件都是同一个文档?如何检索文件?实际上,我只使用字节数组。它们是不同的文件。有趣的是,假设我有abc.jpg和abc.png。它们都会被转换,更新后的文件名都会变成abc.pdf。有趣的是,头中更新的文件名是无用的,因为我需要的只是字节数组。
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++;
}