Created
April 24, 2014 07:16
-
-
Save RhinoLance/11244650 to your computer and use it in GitHub Desktop.
node.js file upload trouble
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){ | |
response.writeHead(200); | |
var destinationFile = fs.createWriteStream(fileName); | |
request.pipe(destinationFile); | |
var fileSize = request.headers['content-length']; | |
var uploadedBytes = 0 ; | |
request.on('data',function(d){ | |
uploadedBytes += d.length; | |
var p = (uploadedBytes/fileSize) * 100; | |
response.write("Uploading " + parseInt(p)+ " %\n"); | |
}); | |
request.on('end',function(){ | |
response.end("File Upload Complete"); | |
}); | |
}).listen(8081,function(){ | |
console.log("server started"); | |
}); |
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'); | |
var options = { | |
hostname: 'localhost', | |
port: 8081, | |
path: '/', | |
method: 'POST' | |
}; | |
var post = http.request(options, function(req) { | |
req.setEncoding('utf8'); | |
req.on('data', function (chunk) { | |
console.log('Response: ' + chunk); | |
}); | |
req.on('error', function(e) { | |
console.log('problem with request: ' + e.message); | |
}); | |
}); | |
var stream = fs.createReadStream('./photoSeed_large.jpg'); | |
stream.pipe(post); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment