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?
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):