Search code examples
c#pdfasp.net-web-apicontent-dispositionhttpresponsemessage

How to Return Files (PDF) From asp.net web api 2


I would like to get pdf file from asp.net web api 2 / c#

here is my Controller

[JwtAuthentication]
[HttpGet]
[Route("Document")]
public IHttpActionResult GetDocument([FromUri]int IdDocument, [FromUri]int ContentFileType)
{
    string filePath = "C:\\xxxxxxxxx\\pdf\\14076186.pdf";
    if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath) || IdDocument == 0)
    {
        return NotFound();
    }

    if (ContentFileType == 0 || ContentFileType == 4)
    {
        byte[] dataBytes = File.ReadAllBytes(filePath);
        MemoryStream pdfDataStream = new MemoryStream(dataBytes);
        string pdfName = IdDocument + "pdf";
        return new PdfResult(pdfDataStream, Request, pdfName);
    }
    else
    {
        HttpResponseMessage responseMsg = new HttpResponseMessage(HttpStatusCode.OK);
        responseMsg.Content = new StreamContent(new FileStream(filePath, FileMode.Open, FileAccess.Read));
        return ResponseMessage(responseMsg);
    }
}

here is my PdfResult class

public class PdfResult : IHttpActionResult 
{
    private readonly MemoryStream PdfStuff;
    private readonly string PdfFileName;
    private readonly HttpRequestMessage httpRequestMessage;
    private HttpResponseMessage httpResponseMessage;

    public PdfResult(MemoryStream data, HttpRequestMessage request, string filename)
    {
        PdfStuff = data;
        httpRequestMessage = request;
        PdfFileName = filename;
    }
    public System.Threading.Tasks.Task<HttpResponseMessage> ExecuteAsync(System.Threading.CancellationToken cancellationToken)
    {
        httpResponseMessage = httpRequestMessage.CreateResponse(HttpStatusCode.OK);
        httpResponseMessage.Content = new StreamContent(PdfStuff);
        //httpResponseMessage.Content = new ByteArrayContent(PdfStuff.ToArray());
        httpResponseMessage.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
        httpResponseMessage.Content.Headers.ContentDisposition.FileName = PdfFileName;
        httpResponseMessage.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream"); // application/pdf
        return System.Threading.Tasks.Task.FromResult(httpResponseMessage);
    }
}

and when i run this on postman i get this (I have GZip Compression enabled)

{{url}}/document?idDocument=14076186&contentFileType=0

%PDF-1.3
1 0 obj
[/PDF /Text /ImageB /ImageC /ImageI
]
endobj
15 0 obj
<< /Length 3306 /Filter /FlateDecode >> stream 
X   �Z�R����U��gm���R��/��0d�
�!f��x.X3`{�&����[�;�-�f$`Į�OO����������,ei�2���0+t�3�ʂ��f��&�:��
    ]~d.qR1�$�Vmy�S˄J�4���#�q�d�3��D    ø_�1�ne|�3��w����?`�G��
n���Eb�c��@Ɓ�l0b;{�QY,�����
����    �f�Rp���hv��    ����@wIJD`��tB6ʼnqrc��    ����iH(�B\k �I��d��萣R�J�je;I�)�%.����R��X�v��&�+���n�X��v�3e.�v6Q nM��e`����b�6Ê�։"�W,e_�<�d\"S�Y~��ϊ�X����5X�^,�J,�P��V8�Q��Q�pL��UeS���됙!�L+����4wj���ښ��D��1k>U���f������������E1Y��|�|\�h�Z�n?+,��p��(�!��8�h��.K?�Z!�2��:cB<�Z�&&���c&������o�������2Χ-�J�DXd�<k�\�l���)��(����MO�Kv�,�O[\u
�f��*��{s�d��M�>��\H�(N&�L���W�K����3&�Vr�nM�'oneDC�je;*�����d�V6����^e��Y
        ]��H�x�jKx�i� ��x�f�{��?�{xܳuŖ���ڽϋEQ>���|�r��#���b��ʡBk�!�sq���Rl�
            }�%*k�;=y��m��T
�β'pPs��a�Wj�yE������jgq���g7�r���h
�<YT���

Can any one help me please ?


Solution

  • The problem is in the Deflate and Gzip compression

    I remove the compression for the action that run the PDF file and it works

    Thanks for your reply