Created
October 14, 2016 07:03
-
-
Save jessemoon0/a29031f16f153bde8fe93e93f193d3e1 to your computer and use it in GitHub Desktop.
Testing Node: Uploading a file, see its progress % and creating an equal size .md file
This file contains hidden or 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
var http = require('http'); | |
var fs = require('fs'); | |
http.createServer(function(request, response){ | |
var newFile = fs.createWriteStream("big_copy.md"); //This is created automatically | |
var fileBytes = request.headers['content-length']; //Check how much bytes a file has | |
var uploadedBytes = 0;//Keep track of the bytes uploaded. | |
request.on('readable', function(){ //Each time information is read | |
var chunk = null; | |
while(null !== (chunk = request.read())){ //Reads each request's chunks | |
uploadedBytes += chunk.length; | |
var progress = (uploadedBytes/fileBytes) * 100; //Calculate upload progress | |
response.write("progress: " + parseInt(progress, 10) + "%\n"); //Show it in browser | |
} | |
}); | |
request.pipe(newFile); //Takes care of the upload for us... | |
request.on('end', function(){ | |
response.end('File Uploaded!'); | |
}); | |
}).listen(8080); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
--> Test with curl --upload big.mp4 http://localhost:8080 where big.mp4 is a video named like that
--> To run curl command, must be on the container folder, file must name as in command and check copied file at the end
--> It only works with .md files