node通过http.request向其他服务器上传文件
2024-08-25 22:18:15
function upload(callback) {
let boundaryKey = '----' + new Date().getTime(); // 用于标识请求数据段
let options = {
host: 'localhost', // 远端服务器域名
port: 80, // 远端服务器端口号
method: 'POST',
path: `/upload`, // 上传服务路径
headers: {
'Content-Type': 'multipart/form-data; boundary=' + boundaryKey,
'Connection': 'keep-alive'
}
};
let req = http.request(options, function(res){
res.setEncoding('utf8'); res.on('data', function(chunk) {
console.log('body: ' + chunk);
}); res.on('end', function() {
console.log('res end.');
});
});
/*req.write(
'--' + boundaryKey + 'rn' +
'Content-Disposition: form-data; name="upload"; filename="test.txt"rn' +
'Content-Type: text/plain'
);*/
req.write(
`--${boundaryKey}rn Content-Disposition: form-data; name="${self.path}"; filename="${self.file}"rn Content-Type: text/plain`
); // 创建一个读取操作的数据流
let fileStream = fs.createReadStream(this.filePath);
fileStream.pipe(req, {end: false});
fileStream.on('end', function() {
req.end('rn--' + boundaryKey + '--');
callback && callback(null);
});
}
参考地址:http://blog.csdn.net/haiyan2012/article/details/8540802
http://www.cnblogs.com/king_domain/p/5630665.html
最新文章
- 日货EmEditor的使用小技巧
- 使用AjaxPro实现无刷新更新数据
- serialVersionUID的作用
- 提升VMware虚拟机性能招数
- 【BZOJ-1076】奖励关 概率与期望 + 状态压缩DP
- ios取证
- 关于mybatis组合查询的分析
- HTML5 webSQL
- Codeforces Round #197 (Div. 2) : A
- iOS远程消息推送自我整理版
- Jquery中index()问题
- 单片机串口通讯RXD与TXD如何对接详解
- Redis中5种数据结构的使用场景
- Zygote及System进程启动
- mysql 开发进阶篇系列 7 锁问题(innodb锁争用情况及锁模式)
- Django 基模板布局设置
- java对redis的基本操作(初识)
- Android中去掉标题栏
- 边界扫描(boundary scan)
- day4 装饰器深入解析