Skip to content Skip to sidebar Skip to footer

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

  1. I only needed FormData() - it takes care of the file and hidden fields.
  2. 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()?"