简体   繁体   English

使用FileContentResult返回文件并将其写入文件

[英]Getting file returned using FileContentResult and writing it to a file

Right now, I have a web service (.asmx) that calls a mvc 4 controller action returning a PDF file via: 现在,我有一个Web服务(.asmx)调用mvc 4控制器操作,通过以下方式返回PDF文件:

return  File(pdfBytes, "application/pdf", "Report.pdf");

And I'm getting the result in the web service like this: 我在Web服务中得到的结果如下:

 WebResponse response = request.GetResponse();
 var status = ((HttpWebResponse)response).StatusDescription;
 var dataStream = response.GetResponseStream();
 StreamReader reader = new StreamReader(dataStream);
 string responseFromServer = reader.ReadToEnd();

What I want is to write the file returned into the file system. 我想要的是将返回的文件写入文件系统。 Something like this: 像这样的东西:

byte[] responseBytes = Encoding.UTF8.GetBytes(responseFromServer);

var file = File.Create("C://Report.pdf");
file.Write(responseBytes, 0, responseBytes.Length);

But the file written has all the pages in blank. 但是编写的文件将所有页面都空白。

What am I missing here? 我在这里错过了什么?


The solution to this was to get the bytes directly from the response stream. 解决方案是直接从响应流中获取字节。

Like this: 像这样:

var dataStream = response.GetResponseStream() ;

byte[] responseBytes;


using (var memoryStream = new MemoryStream())
{
     dataStream.CopyTo(memoryStream);
     responseBytes = memoryStream.ToArray();

     return responseBytes;
}

Don't use the WebResponse object for this. 不要使用WebResponse对象。 Use the WebClient instead. 请改用WebClient It is great for downloading binary data over http . 通过http下载二进制数据非常棒。 All you need to do is replace your webservice´s code with: 您需要做的就是用以下内容替换您的webservice代码:

var serverUrl = "http://localhost:60176/Demo/PdfFile"; //... replace with the request url

var client = new System.Net.WebClient();
var responseBytes = client.DownloadData(serverUrl);
System.IO.File.WriteAllBytes(@"c:\Report.pdf", responseBytes);

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我无法使用 JQuery 和 FileContentResult 打开 CSV 文件 - Why I cannot open a CSV file using JQuery and FileContentResult 找不到文件时处理FileContentResult - Handling FileContentResult when file is not found 使用 FileContentResult 下载文件并使用 javascript 将其存储在客户端 - Download file with FileContentResult and store it client side using javascript FileContentResult操作产生不完整的文件 - FileContentResult action yielding incomplete file FileContentResult 返回 excel 文件损坏 - FileContentResult return excel file corrupt C#FileContentResult文件下载到自定义文件夹中 - C# FileContentResult File Download In Custom Folder Chrome在下载长文件名时修改它们(使用c#FileContentResult) - Chrome modifying long file names when download them (using c# FileContentResult) 从AJAX帖子中调用时,FileContentResult不生成文件 - FileContentResult not generating file when called from AJAX post 将视频文件作为“FileContentResult”流式传输:无法转发/后退视频内容 - Streaming video file as 'FileContentResult': Not able to forward/backward the video content 如何在 ASP.NET WebAPI 中返回文件 (FileContentResult) - How to return a file (FileContentResult) in ASP.NET WebAPI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM