如何 Return 来自 asp.net 网络 api 的文件 (PDF) 2

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

我想从 asp.net web api 2 / c#

获取 pdf 文件

这是我的控制器

[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);
    }
}

这是我的 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);
    }
}

当我在邮递员上 运行 我得到了这个(我启用了 GZip 压缩)

{{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���

谁能帮帮我吗?

您的回复看起来像是 PDF。

您可以尝试保存来自 'Save Response' -> 'Save to a file' 的回复吗?

使用 .pdf 扩展名保存文件。

问题在于 Deflate 和 Gzip 压缩

我删除了 运行 PDF 文件的压缩操作

感谢您的回复