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+'"\r\n\r\n'+value+'\r\n';
}
//文件payload
var getfieldhead=function (field, filename) {
var filefieldhead='content-disposition: form-data; name="'+field+'"; filename="'+filename+'"\r\n'+'content-type: '+getmime(filename)+'\r\n\r\n';
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+'\r\n';
}
//字段格式化
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="\r\n"+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);
/*
------webkitformboundaryuzkmkaovuuysq1dt\r\n
content-disposition: form-data; name="file"; filename="00.jpg"\r\n
content-type: application/octet-stream
\r\n
\r\n +file\r\n
------webkitformboundaryuzkmkaovuuysq1dt\r\n
content-disposition: form-data; name="fieldname"
\r\n
\r\n +value\r\n
------webkitformboundaryuzkmkaovuuysq1dt--
*/
最后这个注释的是标准格式“\r\n”代表实际的字符串,为了看着舒服,视觉上也调整一下
更多node.js模拟浏览器文件上传示例。