如何用nodejs通过post发送multipart/form-data类型的http请求
1个回答
2017-12-05 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
//发送单条消息给接口方app.post("/sendMsgToAByPost",function(req, res, next) {
//我的帖子:http://cnodejs.org/topic/4ffed8544764b729026b1da3
//http://yefeng.iteye.com/blog/315847
//http://stackoverflow.com/questions/5744990/how-to-upload-a-file-from-node-js
//http://stackoverflow.com/questions/9943010/node-js-post-file-to-server
console.log(req.files);
console.log(req.files.media.size);
var boundaryKey = Math.random().toString(16); //随机数,目的是防止上传文件中出现分隔符导致服务器无法正确识别文件起始位置
console.log(boundaryKey);
var options = {
host: 'api.com',
port: 443,
path: '/media?type=image&access_token='+accessToken,
method: 'POST'
};
var reqHttps = https.request(options, function(resHttps) {
console.log("statusCode: ", resHttps.statusCode);
console.log("headers: ", resHttps.headers);
resHttps.on('data', function(body1) {
console.log("body:"+body1);
});
});
var payload = '--' + boundaryKey + '\r\n'
改孙// use your file's mime type here, if known
信歼戚+ 'Content-Type: image/jpeg\r\n'
// "name" is the name of the form field
// "filename" is the name of the original file
+ 'Content-Disposition: form-data; name="media"; filename="aaa.jpg"\r\n'
+ 'Content-Transfer-Encoding: binary\r\n\r\n';
console.log(payload.length);
var enddata = '\r\n--' + boundaryKey + '--';
console.log('enddata:'+enddata.length);
reqHttps.setHeader('Content-Type', 'multipart/form-data; boundary='+boundaryKey+'');
reqHttps.setHeader('Content-Length', Buffer.byteLength(payload)+Buffer.byteLength(enddata)+req.files.media.size);
reqHttps.write(payload);
var fileStream = fs.createReadStream("D:\\aaa.jpg", { bufferSize: 4 * 1024 });
fileStream.pipe(reqHttps, {end: false});
fileStream.on('end', function() {
// mark the end of the one and only part
reqHttps.end(enddata);
});
reqHttps.on('error', function(e) {
console.error("error:"+e);
滑陵 });
});
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询