0

Need your help, I have xhr post request: xhr.send(form.file.files[0]); I'm trying to upload this file to my server in this way :

require('http').createServer(function(req, res) {
    let pathname = decodeURI(url.parse(req.url).pathname);
    let file = new fs.ReadStream(req);
    let path = fs.createWriteStream(__dirname + '/files' + pathname);

    file.pipe(path);

    file.on('error', function(err) {
        console.log(err);
        endConnection(res, 500, 'Server Error');
    });

    res.on('close', file.destroy.bind(file));
}

The main problem is on line new fs.ReadStream(req); - the argument for ReadStream can contain only string, but req is an object. How can I get uploaded file content from the req?

1

2 Answers 2

2

Uploaded file are sent as part of a multipart/form-data request and one usually use a middleware or other utility to get the file. (A request can also send more than one file).

Check out solutions like Multer or multiparty

Sign up to request clarification or add additional context in comments.

Comments

1

try this

let writer = fs.createWriteStream(hereFullNameToYourFileOnServer);

request.pipe(writer);

1 Comment

Yes, that's it. Thanks, Ilia! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.