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); });