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”代表实际的字符串,为了看着舒服,视觉上也调整一下
这段代码只能一次上传一个文件~~
复制代码 代码如下:
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”代表实际的字符串,为了看着舒服,视觉上也调整一下
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
更新日志
2024年11月27日
2024年11月27日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]