6

I've been working to do a PDF file download from bytes[] in ASP.Net MVC C#. The below code is working fine.

I need to convert the code to .NET Core for the same PDF download process.

string fileName = "testFile.pdf";

byte[] pdfasBytes = Encoding.ASCII.GetBytes(fileBytes);   // Here the fileBytes are already encoded (Encrypt) value. Just convert from string to byte
Response.Clear();
MemoryStream ms = new MemoryStream(pdfasBytes);
Response.ContentType = "application/pdf";
Response.Headers.Add("content-disposition", "attachment;filename=" + fileName);
Response.Buffer = true;
ms.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();

I'm tried to convert the above code to .NET Core. I get an error: OutputStream method doesn't contain Response for this ms.WriteTo(Response.OutputStream) line. Thanks In advance.!

Palle Due
  • 5,929
  • 4
  • 17
  • 32
debugger
  • 85
  • 1
  • 1
  • 6
  • 1
    I am quite sure `byte[] pdfasBytes = Encoding.ASCII.GetBytes(fileBytes)` doesn't correctly load the bytes of a (binary!) PDF file – Hans Kesting Aug 05 '19 at 07:17
  • Possible duplicate of [How to convert string to byte array for pdf download in C#](https://stackoverflow.com/q/51810979/11683) – GSerg Aug 05 '19 at 07:21
  • Possible duplicate of [Convert pdf file received in string variable to byte array in C#](https://stackoverflow.com/questions/35242758/convert-pdf-file-received-in-string-variable-to-byte-array-in-c-sharp) – GSerg Aug 05 '19 at 07:23
  • 1
    What **exactly** do you mean by "encrypted"? If the file has somehow been encrypted, will the client know how to use it as a PDF file? – Lasse V. Karlsen Aug 05 '19 at 07:35
  • @Lasse, filebytes variable stored encoded value using by GetStream() method, this value passed string variable to another function. So here i'm convert to byte[]. The pdf is download successfully using above code in .net mvc. this code I want to convert .net core. Response.Outputstream not get in .net core. – debugger Aug 05 '19 at 07:46
  • "Encoding" a PDF file in an ASCII string is going to lose you all the bytes with values higher than 127. Are you using the text PDF format? – Luaan Aug 05 '19 at 07:55
  • Can you show the endpoint where you're using this code? – Lasse V. Karlsen Aug 05 '19 at 09:02

2 Answers2

7

@Luaan sir already gave answer, but it may be my code also help you, so I share it.

Download Pdf file from folder

[HttpGet]
        public FileStreamResult DownloadPdfFile(string fileName)
        {            
            var stream = new FileStream(Directory.GetCurrentDirectory() + "\\wwwroot\\WriteReadData\\" + fileName, FileMode.Open);
            return new FileStreamResult(stream, "application/pdf");
        }

Download pdf file from database

[HttpGet]
        public FileStreamResult DownloadFileFromDataBase(string id)
        {
            var _fileUpload = _db.FileUpload.SingleOrDefault(aa => aa.fileid == id);         // _fileUpload.FileContent type is byte
            MemoryStream ms = new MemoryStream(_fileUpload.FileContent);
            return new FileStreamResult(ms, "application/pdf");
        }

For more info please also see this question and answer. Return PDF to the Browser using Asp.net core

Nripendra Ojha
  • 379
  • 2
  • 14
5

MVC has simplified this. All you need is to have an action that returns ActionResult:

public ActionResult GetImage()
{
  string fileName = "testFile.pdf";
  var pdfasBytes = ...;

  return File(pdfasBytes, "application/pdf", fileName);
}
Luaan
  • 62,244
  • 7
  • 97
  • 116
  • @Luan, code is executed but file not download. Please give any other solution. – debugger Aug 05 '19 at 07:38
  • 1
    @debugger Well, it's not going to do anything if you just execute it in another bit of C# code - the `ActionResult` must be returned from some endpoint. Your original code hacks the request pipeline and rudely breaks the whole thing, so you could execute it almost anywhere, but that's not going to work with this code. Do you have a problem returning the `ActionResult` from a method in the controller properly? It sounds like your MVC app is a quick port from an older WebForms application; do you really want to avoid doing this the MVC way? – Luaan Aug 05 '19 at 07:54