这里还是需要用到第三方库 connect-multiparty
npm install connect-multiparty --save
npm install request-promise --save
var express = require('express');
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
var fs = require('fs');
var rp = require('request-promise');
var router = express.Router();
router.post("/file/upload", multipartMiddleware, function (req, res) {
var file = req.files.file_name; //input type="file" 的 name
var path = file.path;
rp({
uri: 'API',
method: 'POST',
formData: {
file_name: fs.createReadStream(path), // 这里是关键
// 其他需要的字段值,比如 x y w h
},
headers: {
// here
},
json: true
}).then(function (response) {
// 如果后台返回,处理接受response
}).catch(function (response) {
// do something
});
});
网友评论