Search code examples
javascriptajaxform-data

Append string as file upload using FormData


I was wondering if I can upload string as file using form data. I believe there should be some File object, that can have value, filename and maybe also mime-type set.
Pseudo code:

var file = new File();
file.name = "file.txt";
file.mimeType = "text/plain";
file.value = "blah blah\nsecond line";
var data = new FormData();
data.append(file);

Solution

  • works fine for me

    const blob = new Blob(['blah blah\nsecond line'], {type : 'text/plain'})
    formData.append('file', blob, 'file.txt')