.NET Core 6.0 FileContentResult wrong encoding

805 Views Asked by At

I'm trying to view a generated PDF.

public ActionResult Print()
{
   byte[] result = this.PrintDocument();

   Response.Headers.Append("Content-Disposition", "inline; filename=\"" + fileName + "\"");
   return File(result, "application/pdf");
}

The file result corrupted and cannot be opened.

What I obtain is:

%PDF-1.4
%����
4 0 obj
<</Length 680/Filter/FlateDecode>>stream
x��UKO1��^*J��%Yjţ*���VVmԐ7�FS�*���w�u6i e���(����7�o��>
2��2�y��
...

But if I save the result byte array directly in a file this can be proper opened and what I obtain is:

%PDF-1.4
%âãÏÓ
4 0 obj
<</Length 680/Filter/FlateDecode>>stream
xœÅUKO1‰Û^*Jô%YjÅ£*¿½¾VVmÔ7áFS©*¸ôïwìu6i eƒ„ª(»ÞñÌ7ão¾É>
...

Thinking about the charset I tried to change the return File(result, "application/pdf"); line adding, for example, "application/pdf; charset=utf-8" and so on but without any result.

There is a way to avoiding the encoding inside FileContentResult or maybe return directly the result on the Response as usually did in pre-CORE version?

0

There are 0 best solutions below