Patrick McDermott
Patrick McDermott

Reputation: 1220

Sending a document as a Blob to back end using JavaScript

I am sending formData to the backend using an object literal approach like so (which it has to be):

var formData = {};
formData.patentID = caseSelected.patentID;
formData.clientRef = data.clientRef;
formData.amendedDoc = data.amended.amendedDoc;

When the request is made, there is an error. When I review the request payload, the formData.amendedDoc isn't sending the attached PDF and instead provides an empty object like so:

enter image description here

If I log the formData just before sending in the http request, I can see that the PDF file is there

Question

How do I send the PDF document with the rest of the formData? Confused on Blobs and Base64 concepts/

Upvotes: 0

Views: 543

Answers (1)

Naveen Chahar
Naveen Chahar

Reputation: 571

You can't include file inside a JSON while making a request. Instead use FormData object to send to files along with other fields. Like this -

      var formData = new FormData();
      formData.append("patentID",caseSelected.patentID);
      formData.append("clientRef",data.clientRef);
      formData.append("amendedDoc",data.amended.amendedDoc);

Also do not forget to set the content-type of request to multipart/form-data ( Content-Type: multipart/form-data )

Upvotes: 1

Related Questions