问题
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