Something like this for example...
let body = new FormData();
body.append('bucket', options.bucket || '');
body.append('content_type', options.contentType || file.type);
body.append('expires', options.expires || '');
body.append('visibility', options.visibility || '');
let payload = {
method : 'post',
credentials : 'same-origin',
body : body,
headers : {
'X-Requested-With' : 'XMLHttpRequest',
},
};
let response = await fetch('/vapor/signed-storage-url', payload);
let data = await response.json();
let headers = data.headers;
if ('Host' in headers) {
delete headers.Host;
}
data.extension = file.name.split('.').pop();
await function() {
return new Promise(function (resolve, reject) {
let xhr = new XMLHttpRequest();
xhr.open('PUT', data.url);
xhr.onload = () => resolve(xhr.response);
xhr.upload.onprogress = (e) => options.progress(Math.ceil((e.loaded / e.total) * 100));
for (const header in headers) {
xhr.setRequestHeader(header, headers[header]);
}
xhr.send(file);
});
}();
return data;
I used fetch for the first request since there's no need to monitor upload progress and fetch doesn't require additional logic to work with promises unlike XHR.