node.js - nodejs upload file HTTP POST -
i want "client.js" read file, , upload folder via "server.js" using http post. when file size small(1kb), works. when file size bigger(maybe around 100kb), doesen't work. there no error, stored image less in size it's supposed be. don't know why. please help.
1.client.js
var fs = require('fs'); var http = require('http'); postdata = null; postdata=fs.readfilesync("test.jpg") if(postdata!=null){ var options = { host: 'localhost', port: 10730, method: 'post' }; var clientrequest = http.request(options); clientrequest.end(postdata);}
2.server.js
var http = require('http'); var fs = require('fs'); var server = http.createserver((req,res)=>{ req.on('data', (chunk)=>{ fs.writefile('testcopy.jpg',chunk)}) req.on('end', ()=>{ console.log("end") })}) server.listen(10730,'localhost');
thank in advance.
you can use multer, middleware handles multipart/form-data
automatically save file , populates req variable: req.file //access file info
. has lot of functionalities abstract kind of work. can define size of file, filter files , many other facilities, know multer simple use , works express middleware think can try this:
var http = require('http'); var server = http.createserver((req,res)=>{ upload(req, res, function (err) { if (err) { // error occurred when uploading return } console.log('end') // went fine }) }) server.listen(10730,'localhost');
Comments
Post a Comment