這篇文章主要介紹了Node.js模擬浏覽器文件上傳的實現代碼,需要的朋友可以參考下
OSChina上發過了,那個也是我的,現在放到這來,哈哈 代碼如下: var path=require("path"); var fs=require("fs"); var http=require("http"); //post值payload var getfield=function(field, value) { return 'Content-Disposition: form-data; name="'+field+'"rnrn'+value+'rn'; } //文件payload var getfieldHead=function (field, filename) { var fileFieldHead='Content-Disposition: form-data; name="'+field+'"; filename="'+filename+'"rn'+'Content-Type: '+getMime(filename)+'rnrn'; return fileFieldHead; } //獲取Mime var getMime=function (filename) { var mimes = { '.png': 'image/png', '.gif': 'image/gif', '.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; } //獲取邊界檢查隨機串 var getBoundary=function() { var max = 9007199254740992; var dec = Math.random() * max; var hex = dec.toString(36); var boundary = hex; return boundary; } //獲取boundary var getBoundaryBorder=function (boundary) { return '--'+boundary+'rn'; } //字段格式化 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數據 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); }); } //讀取文件 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); }); } //各類設置 var opt={ "url":"http://xxxx.xx",//url "file":"00.jpg",//文件位置 "param":"file",//文件上傳字段名 "field":{//其余post字段 "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-- */ 最後這個注釋的是標准格式“rn”代表實際的字符串,為了看著舒服,視覺上也調整一下