简体   繁体   English

如何从 asp.net web api 2 返回文件 (PDF)

[英]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#我想从 asp.net web api 2 / c# 获取 pdf 文件

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这是我的 PdfResult 类

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)当我在邮递员上运行它时,我得到了这个(我启用了 GZip 压缩)

{{url}}/document?idDocument=14076186&contentFileType=0 {{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 ?任何人都可以帮助我吗?

Your response already looks like it is a PDF.您的回复看起来像是 PDF。

Can you try saving the response from 'Save Response' -> 'Save to a file' from the Postman?您可以尝试从 Postman 的“保存响应”->“保存到文件”中保存响应吗?

Save the file with .pdf extension.使用 .pdf 扩展名保存文件。

邮递员截图以供澄清

The problem is in the Deflate and Gzip compression问题出在 Deflate 和 Gzip 压缩中

I remove the compression for the action that run the PDF file and it works我删除了运行 PDF 文件的操作的压缩,它可以工作

Thanks for your reply感谢您的回复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM