I used following code to upload a image to Google cloud platform(GCP). The upload is successful but it's not opening after download. When inspected through Chrome browser "response" terminal. I see "--WebkitFormBoundar" at the end of the uploaded file which causes it corruped. Any ideas how to fix it?
<form method ="post" enctype="multipart/form-data" accept-charset="UTF-8">
<input title="upload" #fileInput type="file" class="display-none" (change)="onFileChange($event, item)" />
<button title="Manage Files" (click)="manageDialog($event, item)"></button>
<button icon="pi-upload" title="Upload File" (click)="fileInput.click()" ></button>
</form>
Original image size: 44.1 kb
Corrupted image size: 44.3 kb
It looks like dumb question or scenario but this 0.2 kb size diff causes corruption in Multipart/form-data file upload. Thanks for your help.
I was part of original dev team at amazing web time tracker.
It is weird but here is the solution:
Remove the enctype="multipart/form-data"
from HTML form section.
In Javascript POST call, update the code as below:
fetch("http://example.com/path/upload.php", {
method: 'PUT',
body: event.target.files[0],
headers: {
'Content-Type': (event.target.files[0]).type
}
})
If you have formdata, remove it (these lines were responsible for corruption and unintended file size increases):
// const formData = new FormData();
// formData.append('file', file);
Summary:
enctype
formData
which might add "webkit-boundary" garbage text to uploaded file.event.target.files[0]
directly in Fetch/HTTP API's body param and set file-type/content-type properly from (event.target.files[0]).type
'Content-Type': undefined
which may be incorrect and it didn't work out for us.Notes: I'm co-dev at TimeOnSite for web.