Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

222
Visualizações
How to display a PDF bytearray in browser using javascript?

I have been trying the below client side code to render the PDF byte array returned from server side. It shows the correct number of thumbnails/pages in browser but the content is not displaying. Both the thumbnails and the pages are completely black. Is there anything I am missing here? Also, I read that createObjectURL is deprecated. Is there any better way to achieve this?

Thank you!

Server side code:

byte[] bytes = service.GetDocument(ID);

result.StatusCode = HttpStatusCode.OK;
result.Content = new ByteArrayContent(bytes, 0, bytes.Length);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("inline");

Client side code:

@model DocumentWeb.ViewModels.Document

<html>
    <body>

        <object data="" type="application/pdf" width="100%" height="100%">
        </object>

        <script>

            $(document).ready(function (e) {

                $("object").attr("width", screen.availWidth);

                $("object").attr("height", screen.availHeight);

                var url = "@Url.Content("~/pdf/document")";
            
                    var data = {
                        ID: "@Model.DocID",
                    };

                    var result = $.post({
                        url: url,
                        type: 'POST',
                        data: data
                    });

                    result.done(function (bytearray) {

                        var blob = new Blob([bytearray], { type: 'application/pdf' });

                        var link= window.URL.createObjectURL(blob);

                        $("object").attr("data", link);
                    
                    });

                    result.fail(function (xhr) {
                        console.log(xhr.responseText);
                    });
                
            });

        </script>

    </body>
</html>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You could always base64 encode your byte array. HTML <object> tags support base64 encoded images. Something like <object src="data:application/pdf;base64, ABC123... /> where ABC123... is your base64 encoded PDF binary.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda