Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

199
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!