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

最新文章

  1. 日货EmEditor的使用小技巧
  2. 使用AjaxPro实现无刷新更新数据
  3. serialVersionUID的作用
  4. 提升VMware虚拟机性能招数
  5. 【BZOJ-1076】奖励关 概率与期望 + 状态压缩DP
  6. ios取证
  7. 关于mybatis组合查询的分析
  8. HTML5 webSQL
  9. Codeforces Round #197 (Div. 2) : A
  10. iOS远程消息推送自我整理版
  11. Jquery中index()问题
  12. 单片机串口通讯RXD与TXD如何对接详解
  13. Redis中5种数据结构的使用场景
  14. Zygote及System进程启动
  15. mysql 开发进阶篇系列 7 锁问题(innodb锁争用情况及锁模式)
  16. Django 基模板布局设置
  17. java对redis的基本操作(初识)
  18. Android中去掉标题栏
  19. 边界扫描(boundary scan)
  20. day4 装饰器深入解析

热门文章

  1. 设置div背景透明的CSS样式
  2. Vue学习资料
  3. Node.js+Koa开发微信公众号个人笔记(二)响应事件
  4. Bootstrap里的文件分别代表什么意思及其引用方法
  5. Android中的padding和margin的区别
  6. 使用Visual Studio Team Services进行压力和性能测试(二)——压力测试执行
  7. HttpClient与浏览器调用服务接口差异
  8. python第七十六天--堡垒机完成
  9. Django电商项目---完成订单页面day5
  10. VMware虚拟机中CentOS 7的硬盘空间扩容