Last active
May 1, 2024 12:38
-
-
Save PaulMougel/7511372 to your computer and use it in GitHub Desktop.
File upload in Node.js to an Express server, using streams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// node: v0.10.21 | |
// request: 2.27.0 | |
var request = require('request'); | |
var fs = require('fs'); | |
var r = request.post("http://server.com:3000/"); | |
// See http://nodejs.org/api/stream.html#stream_new_stream_readable_options | |
// for more information about the highWaterMark | |
// Basically, this will make the stream emit smaller chunks of data (ie. more precise upload state) | |
var upload = fs.createReadStream('f.jpg', { highWaterMark: 500 }); | |
upload.pipe(r); | |
var upload_progress = 0; | |
upload.on("data", function (chunk) { | |
upload_progress += chunk.length | |
console.log(new Date(), upload_progress); | |
}) | |
upload.on("end", function (res) { | |
console.log('Finished'); | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// node: v0.10.7 | |
// express: 3.4.4 | |
var fs = require('fs'); | |
var express = require('express'); | |
var app = express(); | |
app.post('/', function (req, res, next) { | |
req.pipe(fs.createWriteStream('./uploadFile')); | |
req.on('end', next); | |
}); | |
app.listen(3000); |
thanks
I need to receive video file from post request then need to upload on youtube server. This code is not working in this case. kindly suggest thanks.
options.headers = {
"Content-Type": "application/octet-stream",
"Content-Disposition": "attachment"
};
I would recommend to set the Content-Type in request. Because using express body parsers will stop the data stream and the upload will hang if the streamed data contains json like structures or urls.
THANKS <3
hi I have one doubt how to save the data in the file in express.js
How to to open uploaded file?
The req.on('end', next) means that piping is finished.
What if it is needed to open the file when it is uploaded?
UPD
var upload fs.createWriteStream('./uploadFile')
await req.pipe(upload);
upload.on('close', () => {
// upload is ready
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I try it and its work! Very nice... but what if I want receive a streamed big file from a ejs page?