I
I
ikerya2019-04-29 18:59:39
Node.js
ikerya, 2019-04-29 18:59:39

How can I upload a file using form-data that has the body specified as a string?

When I try to upload a file to mysite.com/upload like this, that site thinks the body is empty:

const FormData = require('form-data');
const form = new FormData();

form.append('file', 'file content');
form.submit('http://mysite.com', (err, res) => {
    ...
});

But if you read the file from disk through the stream, then everything is successfully loaded with the desired content:
const fs = require('fs');
const FormData = require('form-data');
const form = new FormData();

form.append('file', fs.createReadableStream('path/to/file'));
form.submit('http://mysite.com', (err, res) => {
    ...
});

Why does the first option work incorrectly?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question