node 实现把文件上传到另一台服务器
Posted 大智ywz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了node 实现把文件上传到另一台服务器相关的知识,希望对你有一定的参考价值。
1、needle
npm install needle
var needle = require(‘needle‘); var data = { foo: ‘bar‘, image: { file: ‘./images/2.png‘, content_type: ‘image/png‘ } } needle.post(‘http://xxx/upload_v_article‘, data, { multipart: true }, function(err, resp, body) { // needle will read the file and include it in the form-data as binary console.log(err, resp, body) });
2、request
npm install request
var request = require(‘request‘); var formData = { // Pass a simple key-value pair my_field: ‘my_value‘, // Pass data via Buffers my_buffer: new Buffer([1, 2, 3]), // Pass data via Streams my_file: fs.createReadStream(‘./images/2.png‘), }; request.post({url:‘http://xxx/upload_v_article‘, formData: formData}, function optionalCallback(err, httpResponse, body) { if (err) { return console.error(‘upload failed:‘, err); } console.log(‘Upload successful! Server responded with:‘, body); });
以上是关于node 实现把文件上传到另一台服务器的主要内容,如果未能解决你的问题,请参考以下文章