码迷,mamicode.com
首页 > Web开发 > 详细

node 实现把文件上传到另一台服务器

时间:2017-11-10 18:53:39      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:led   value   with   target   buffers   hub   ref   option   streams   

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 实现把文件上传到另一台服务器

标签:led   value   with   target   buffers   hub   ref   option   streams   

原文地址:http://www.cnblogs.com/yangwenzhi/p/7815417.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!