0

For learning purposes, I'd like to know how to create a FormData object.

Using the FormData api, I can send a file like this :

        let data = new FormData();
        data.append('avatar', avatar);

        var xhr = new XMLHttpRequest();
        xhr.open('POST', '/api_enpoint', true);
        xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
        xhr.setRequestHeader('X-CSRF-TOKEN', document.head.querySelector('meta[name="csrf-token"]').content);
        xhr.send(data);

"avatar", appended to the FormObject, is a "File" object.

How can I perform the "FormData" work manually ? That is, creating the full object, and send it with xhr.send(data)

Is there an example somewhere ? I can't find code that works.

I've read the RFC, but it does not help me that much.

Edit: My question is different than How to manually create multipart/form-data

My case is specifically for sending a file. And the other question does not handle that. There's no working solution at all for both questions.

Community
  • 1
  • 1
trogne
  • 3,402
  • 3
  • 33
  • 50
  • 1
    Possible duplicate of [How to manually create multipart/form-data](https://stackoverflow.com/questions/47080869/how-to-manually-create-multipart-form-data) – Heretic Monkey Aug 02 '18 at 20:41
  • It's not a duplicate. There is no working solution for sending a file object. And my case is specifically for sending a file – trogne Aug 02 '18 at 22:37

0 Answers0