簡體   English   中英

Pdf 來自 web api 服務未打開

[英]Pdf from web api service not opening

下面是我的代碼,實際上它會下載 pdf 文件,但它可能已損壞或有問題。 你能幫我解決這個問題嗎?

public Task<HttpResponseMessage> ExportPDF()
    {
        HttpResponseMessage response =
           Request.CreateResponse(System.Net.HttpStatusCode.BadRequest, "No data to export.");
        MemoryStream _stream = new MemoryStream();
        try
        {
            _stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes("da adasdasd adasd"));
            //if file the dowload
            if (_stream.Length > 0)
            {
                response = Request.CreateResponse(HttpStatusCode.OK);
                response.Content = new StreamContent(_stream);
                response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("inline");
                response.Content.Headers.ContentLength = _stream.Length;
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
                response.Content.Headers.ContentDisposition.FileName = "Responses.pdf";
            }
            return Task.FromResult(response);
        }
        catch (Exception ex)
        {
            return Task.FromResult(response); 
        }
    }

以下是結果: 在此處輸入圖像描述

您在 MemoryStream 中生成一個文本文件。 如果您將“application/pdf”更改為“text/plain” ,它應該可以工作。

如果你真的想在 MemoryStream 中生成一個 PDF 文件,有一些庫可以幫助你做到這一點,比如 IronPdf、Aspose、...

謝謝大家,我使用了“SelectPDF”庫,現在工作正常!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM