reqwest icon indicating copy to clipboard operation
reqwest copied to clipboard

Post a file using reqwest

Open Waterlog opened this issue 11 years ago • 5 comments

I've a form like:

<form action="../" class="formie">
    <input type="text" placeholder="Your name" name="namie">
    <input type="file" name="filie">
    <button type="submit">Submit</button>
</form>

And some JS:

var form = document.querySelector('.formie');

if (form) {
    // when input's value is changed, object's value is updated
    var values = {
        name: '',
        filie: ''
    };

    // https://github.com/desandro/eventie 
    eventie.bind(form, 'submit', function(event) {
        reqwest({
                url: form.getAttribute('action'),
                method: 'post',
                data: values, 
                success: function(resp) {
                    alert('submitted');
                }
        });

        return false;
    });
})

So I've a value of the <input type="file">.

The question is, how do I send a file to the server using reqwest?

Waterlog avatar Mar 17 '14 08:03 Waterlog

Any success or update ?

darkyen avatar Dec 18 '14 13:12 darkyen

+1

petrkomarek avatar Aug 25 '15 21:08 petrkomarek

+1

EwanValentine avatar Sep 01 '15 14:09 EwanValentine

+1

shivekkhurana avatar Sep 03 '15 07:09 shivekkhurana

This works for me:

let formData = new FormData();
formData.append('file', file);

reqwest({
  url: API_URL,
  method: 'post',
  crossOrigin: true,
  processData : false,
  data : formData
}).then(res => { console.log(res); });

processData:false seems to be the important option

moklick avatar Jan 19 '16 01:01 moklick