How to convert string to byte array for pdf download in C#

为君一笑 提交于 2019-12-11 06:04:30

问题


I am working with .NET MVC application in that I need to implement PDF download functionality. I know how to download file using existing file but my problem is I am receiving pdf file content as string from SOAP service.

PDF Content I am receiving is like :

%PDF-1.4
%Óëéá
1 0 obj
<</Creator (Mozilla/5.0 \(Windows NT 6.1; Win64; x64\) AppleWebKit/537.36 \(KHTML, like Gecko\) Chrome/65.0.3325.181 Safari/537.36)
/Producer (Skia/PDF m65)
/CreationDate (D:20180407105711+00'00')
/ModDate (D:20180407105711+00'00')>>
endobj
2 0 obj
<</Filter /FlateDecode
/Length 3817>> stream
xœÕ›ÛŽ$9†
..........

I have following code to download file:

public FileResult Download()
{
    byte[] fileBytes = System.IO.File.ReadAllBytes(@"c:\folder\myfile.ext");
    string fileName = "myfile.ext";
    return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, 
    fileName);
}

But how to convert file content string to byte array?

I tried convert string to byte array using MemoryStream and

byte[] bytes = Encoding.ASCII.GetBytes(someString);

but in that case PDF reader is showing error that file is corrupted.

来源:https://stackoverflow.com/questions/51810979/how-to-convert-string-to-byte-array-for-pdf-download-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!