How Do I Append A File To Formdata()?
fd.append('upload', file) yields, ------WebKitFormBoundaryJnjpATRkxe2Duwwu Content-Disposition: form-data; name='userid' 8022171621665209152 ------WebKitFormBoundaryJnjpATRkxe2Duw
Solution 1:
I found the sources of my errors
- I only needed
FormData()
- it takes care of the file and hidden fields. - I was overwriting
Content-Type
. Form boundaries were lost when I did this:
`xhr.setRequestHeader("Content-Type", "multipart/form-data")
The corrected code is below (along with some context):
app.ports.uploadFile.subscribe(function(pathAndId){
var [tasksrvPath, formId] = pathAndId
try {
var fd = newFormData(document.getElementById(formId));
var r = newXMLHttpRequest()
r.open("POST", tasksrvPath, true)
r.setRequestHeader("Authorization", "Bearer " + token() )
r.send(fd)
r.onload = function() {
app.ports.status.send(r.statusText)
}
} catch(e) {
console.log(e.message);
}
})
Post a Comment for "How Do I Append A File To Formdata()?"