Skip to content

JavaScript file upload #4900

@stanmertens

Description

@stanmertens

Hello,

I'm trying to upload files to Flask using Javascript. On this page I can read how to do that with an HTML form. However I'm not able to do the same with pure JavaScript.

To replicate:

const form = new FormData();
const files = document.getElementById('media-files').files;
for (let i = 0; i < files.length; i++) {
    form.append(`file[${i}]`, files[i]);
}
await fetch('/api/v1/media', {
    method: 'POST',
    body: form,
    headers: {'Content-Type': 'multipart/form-data'}
});

In Flask request.data is not empty. But request.files and request.form are.

Any help would be greatly appreciated.

Environment:

  • Python version: 3.11
  • Flask version: 2.2.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions