как я могу открыть pdf в браузере вместо загрузки в webapi

Я использую selectPdf для создания динамических PDF-файлов в моем webapt. Выберите PDF, загрузите этот PDF как вложение, я хочу, чтобы этот PDF отображался на вкладке браузера, а не загружался. следующий мой код.

   [HttpGet]
    [Route("myapplication")]
    [AllowAnonymous]
private System.Net.Http.HttpResponseMessage GeneratePDF(string guid, bool isAr, out string fileName)
    {
        string htmlString = ApplicationService.GeneratePDFHTMLString(guid, isAr, out fileName);
        string pdf_page_size = "A4";
        PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize),
            pdf_page_size, true);


        string pdf_orientation = "Portrait";
        PdfPageOrientation pdfOrientation =
            (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation),
            pdf_orientation, true);

        int webPageWidth = 748;


        int webPageHeight = 0;


        // instantiate a html to pdf converter object
        HtmlToPdf converter = new HtmlToPdf();


        // set converter options
        converter.Options.PdfPageSize = pageSize;
        converter.Options.PdfPageOrientation = pdfOrientation;
        converter.Options.WebPageWidth = webPageWidth;
        converter.Options.MarginTop = 20;

        converter.Options.WebPageHeight = webPageHeight;
        converter.Options.ExternalLinksEnabled = true;

        converter.Options.EmbedFonts = true;
        converter.Options.KeepTextsTogether = true;

        try
        {
            // create a new pdf document converting an url
            PdfDocument doc = converter.ConvertHtmlString(htmlString);
            doc.CompressionLevel = PdfCompressionLevel.NoCompression;

            //var contents = doc.Save();

            //doc.Close();
            //return contents;

            MemoryStream pdfStream = new MemoryStream();

            // save pdf document into a MemoryStream
            doc.Save(pdfStream);

            // reset stream position
            pdfStream.Position = 0;

            System.Net.Http.HttpResponseMessage response = new System.Net.Http.HttpResponseMessage();
            response.StatusCode = System.Net.HttpStatusCode.OK;
            //response.Content = new System.Net.Http.StreamContent(pdfStream);
            response.Content = new System.Net.Http.ByteArrayContent(pdfStream.ToArray());
            response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
            {
                FileName = "foo.pdf"
            };
            response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
            doc.Close();
            return response;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

person Muhammad Saifullah    schedule 13.08.2018    source источник
comment
Возможный дубликат Как заставить ли файлы открываться в браузере вместо загрузки (PDF)?   -  person Severin Jaeschke    schedule 13.08.2018
comment
Вас также может заинтересовать это   -  person Severin Jaeschke    schedule 13.08.2018


Ответы (1)


    [HttpGet]
    [Route("myapplication")]
    [AllowAnonymous]
    private System.Net.Http.HttpResponseMessage GeneratePDF(string guid, bool isAr, out string fileName)
        {
            string htmlString = ApplicationService.GeneratePDFHTMLString(guid, isAr, out fileName);
            string pdf_page_size = "A4";
            PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize),
                pdf_page_size, true);
    
    
            string pdf_orientation = "Portrait";
            PdfPageOrientation pdfOrientation =
                (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation),
                pdf_orientation, true);
    
            int webPageWidth = 748;
    
    
            int webPageHeight = 0;
    
    
            // instantiate a html to pdf converter object
            HtmlToPdf converter = new HtmlToPdf();
    
    
            // set converter options
            converter.Options.PdfPageSize = pageSize;
            converter.Options.PdfPageOrientation = pdfOrientation;
            converter.Options.WebPageWidth = webPageWidth;
            converter.Options.MarginTop = 20;
    
            converter.Options.WebPageHeight = webPageHeight;
            converter.Options.ExternalLinksEnabled = true;
    
            converter.Options.EmbedFonts = true;
            converter.Options.KeepTextsTogether = true;
    
            try
            {
                // create a new pdf document converting an url
                PdfDocument doc = converter.ConvertHtmlString(htmlString);
                doc.CompressionLevel = PdfCompressionLevel.NoCompression;
    
                //var contents = doc.Save();
    
                //doc.Close();
                //return contents;
    
                MemoryStream pdfStream = new MemoryStream();
    
                // save pdf document into a MemoryStream
                doc.Save(pdfStream);
    
                // reset stream position
                pdfStream.Position = 0;
    
                System.Net.Http.HttpResponseMessage response = new System.Net.Http.HttpResponseMessage();
                response.StatusCode = System.Net.HttpStatusCode.OK;
                //response.Content = new System.Net.Http.StreamContent(pdfStream);
                response.Content = new System.Net.Http.ByteArrayContent(pdfStream.ToArray());
                 //response.Content.Headers.ContentDisposition = new 
                 // System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
                 // {
                 //      FileName = "foo.pdf"
                 //  };
                response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
                doc.Close();
                return response;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Попробуй это

person sherox    schedule 07.10.2020