This article mainly introduces the Node.js simulation browser file upload implementation code, the need for friends can refer to the following
Oschina, that's mine, now put it here, haha code is as follows: Var path=require ("path"); var fs=require ("FS"); var Http=require ("http"); //post value payload var getfield=function (field, value) { return ' Content-disposition:form-data; Name= "' +field+ '" rnrn ' +value+ ' rn '; } //File payload var getfieldhead=function (field, filename) { var filefieldhead= ' Content-disposition:form-data; Name= "' +field+ '"; Filename= "' +filename+ '" rn ' + ' Content-type: ' +getmime (filename) + ' rnrn '; return filefieldhead; } / Get mime var getmime=function (filename) { var mimes = { '. png ': ' Image/png ', '. gif ': ' Image/gif ', &N Bsp '. jpg ': ' image/jpeg ', '. jpeg ': ' Image/jpeg ', '. js ': ' Appliction/json ', '. Torrent ': ' application/ Octet-stream ' }; var ext = path.extname (filename); var mime = mimes[ext]; mime=!! Mime?mime: ' Application/octet-stream '; return mime; } //Get boundary check random string var getboundary=function () { var max = 9007199254740992; var dec = math.random () * max; var hex = dec.to String; var boundary = hex; return boundary; } //Get boundary var getboundaryborder= function (boundary) { return '--' +boundary+ ' rn '; } //Field format function fieldpayload (opts) { var payload=[]; for (Var ID in Opts.field) { Payload.push (GetField (id,opts.field[id)); } Payload.push (""); return Payload.join (Getboundaryborder (opts.boundary)); } //post data function Postrequest (opts) { Filereadstream (opts,function (buffer) { var options=require (' url '). Parse ( Opts.url); var header={}; var h=getboundaryborder (opts.boundary); var e=fieldpayload (opts); var a=getfieldhead (opts.param,opts.file); var d= "rn" +h; header["Content-length"]=buffer.bytelength (h+e +a+d) +buffer.length; header["Content-type"]= ' Multipart/form-datA boundary= ' +opts.boundary; options.headers=header; options.method= ' POST '; var req=http.request ( Options,function (res) { var data= '; res.on (' Data ', function (chunk) { data+=chunk; }); Res.on (' End ', function () { console.log (res.statuscode) Console.log (data); }); }); Req.write (H+e+a); Log.diy (h+e+a+buffer+d); req.write (buffer); req.end (d); }); } Read file function Filereadstream (opts, FN) { var readstream = fs.createreadstream (opts.file,{flags: ' R ', Encoding:null}); var chunks=[]; var length = 0; readstream.on (' Data ', function (chunk) { length = chunk.length; Chunks.push (chunk); }); Readstream.on (' End ', function () { var buffer = new Buffer ( Length); for (var i = 0, pos = 0, size = Chunks.length i < size; i++) { chunks[i].copy (buffer, POS); POS + + chunks[i].length; } fn (buffer); }); &nBsp } //Various settings var opt={ "url": "http://xxxx.xx",//url "file": "00.jpg",//File location "param": " file,//Files upload field name "field": {//The remaining post fields "Client": "1", "title": "OK" }, "boundary": "---- Webkitformboundary "+getboundary () } postrequest (opt); /* ------ webkitformboundaryuzkmkaovuuysq1dtrn Content-disposition:form-data; Name= "File"; Filename= "00.jpg" rn content-type:application/octet-stream rn rn +filern ------ webkitformboundaryuzkmkaovuuysq1dtrn Content-disposition:form-data; Name= "FieldName" rn RN +valuern ------webkitformboundaryuzkmkaovuuysq1dt-- */ Finally this annotation is the standard format "RN" represents the actual string, in order to look comfortable, also visually adjust the