I've been following this FormData tutorial here, however have yet to understand how the formData object works.
My input form
<input type="file" id="file-id" class="w300px rounded4px" name="file" placeholder="PDF file">
<button class="btn-upload-pdf" ng-click="asub.uploadPDF()">Upload</button>
Here is the upload button function:
this.uploadPDF = () => {
const formData = new FormData();
const fileInput = document.getElementById('file-id');
const file = fileInput.files[0];
formData.append('pdf-file', file);
console.log('formData', formData)
return ApiFactory.insightPDF(formData).then((res) => {
console.log('res', res);
return res;
});
};
When I log out the fileInput
object .files[0]
I see the file I just attached:
It would seem to mean that this object should be enough to send along to the POST. However this is the next step:
formData.append('pdf-file', file);
I log out formData
before I send it into my Factory and this is the result, I don't see the key pdf-file
or that PDF anywhere, just a bunch of methods? Where does the file get appended too? How does formData contain the actual PDF?
I need to attach something from the formData object I presume:
The Factory that makes the POST request
const insightPDF = (formData) => {
console.log(' formData', formData)
return $http.post('/app/api/insights_pdf', formData).then((res) => {
console.log('PDF uploaded res', res)
return res;
}).catch(apiError);
};