node-formidable / formidable

The most used, flexible, fast and streaming parser for multipart form data. Supports uploading to serverless environments, AWS S3, Azure, GCP or the filesystem. Used in production.
MIT License
7.01k stars 680 forks source link

Formidable does not handles form-data using ajax #323

Closed manuel-di-iorio closed 9 years ago

manuel-di-iorio commented 9 years ago

If you test this code on your server, you will see on the node.js console that the "field" and "value" outputs a long text that includes the unparsed --webkit-boundary, etc..

Test case:

index.html

<form onsubmit="return false;" id="form">
    <input type="text" name="text1" value="hello"><br>
    <input type="text" name="text2" value="friend!"><br>
    <input type="submit" onclick="send()">
</form>

<script>
function send() {
    var form = document.getElementById("form"),
        formData = new FormData(form);
        xhr = new XMLHttpRequest();

    xhr.open("POST", "/");  
    xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
    xhr.send(formData);
}
</script>

formidable js code in my server

var form = new formidable.IncomingForm();

form.parse(req, function(err, fields, files) {
      if (err) console.log(err);
});

form.on('field', function(field, value) {
     console.log("field: ", field);
     console.log("value: ", value);
});

The output on the console should be:

field: text1
value: hello
field: text2
value: friend!

Instead, the output is:

field:  ------WebKitFormBoundaryqupdxVjAIPM03FAF
Content-Disposition: form-data; name
value:  "upload"

hello
------WebKitFormBoundaryqupdxVjAIPM03FAF
Content-Disposition: form-data; name="testo"

friend!
------WebKitFormBoundaryqupdxVjAIPM03FAF--

This situation does not happens if you doesn't use form data, e.g. directly putting the data in xhr.send() instead of formData

gabeio commented 9 years ago

try adding form.multiples = true; before form.parse

manuel-di-iorio commented 9 years ago

Ok now works, sorry the problem was xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");

I have Formidable as dependance in my project, compliments for the good work!

gabeio commented 9 years ago

Just glad to hear you got it working :smile:

yoyousuf67 commented 6 years ago

Thanks a lot.