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

202
Visualizações
Append PDF binary string to JS FormData
var uploadData = new FormData();
uploadData.append('myFile', pdfData, fileName);

pdfData is coming from another server and outputs following on the console (excerpt):

%PDF-1.3 %¿÷¢þ 1 0 obj << /Metadata 3 0 R /Pages 4 0 R /Type /Catalog >> endobj 2 0 obj

so it's a PDF file in binary string (?).

Executing this code leads to following exception:

Uncaught TypeError: Failed to execute 'append' on 'FormData': parameter 2 is not of type 'Blob'.

When trying this:

uploadData.append('myFile', new Blob([pdfData], {type: 'application/pdf'}, fileName);

the request goes through but I get errors on the server side:

java.io.IOException: java.util.zip.DataFormatException: invalid code lengths set

So the PDF is sent not correctly, I assume.

How to encode pdfData correctly?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

check if your Blob instance contains a valid PDF file. Like this:

const blob = new Blob([data], { type: file.mime_type });
const blob_url = URL.createObjectURL(blob);

and then create an element to view it:

const embed_element = document.createElement('embed');
embed_element.setAttribute('src', blob_url);
embed_element.setAttribute('width', '800px');
embed_element.setAttribute('height', '2100px');
document.body.append(embed_element);

Does it show the PDF file correctly?

You should also check in your developer tools if the request is sent correctly. I think it should look similar to this (raw FormData in Chrome):

enter image description here

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