upload.js
2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var request = require('request');
var _ = require('lodash');
var fs = require('fs');
var ipaddr = require('ipaddr.js');
var domain = require('../../config/common.js').domain;
//var domain = 'http://172.16.6.227:8083/yohobuy-platform-web'; //马力
// var domain = 'http://172.16.6.162:8088/platform';
//var domain = 'http://172.16.6.240:8088/platform';
exports.uploadFile = function(req, res) {
var headers = {};
if (req.session && req.session.user) {
var ipObject = ipaddr.process(req.ip).octets
var ip = ipObject ? ipObject.join('.') : req.ip;
headers = {
'x-user-id': req.session.user.auth.pid,
'x-user-name': req.session.user.auth.account,
'x-site-type': 1,
'x-client-ip': ip
};
}
if (req.is("multipart/form-data")) {
console.log(req.body);
req.body.file = fs.createReadStream(req.files[req.body.filename].path);
if (req.body.__type == "batch-import") {
request.post({
url: domain + '/batch/import',
formData: req.body,
headers: headers
}, function optionalCallback(error, httpResponse, body) {
if (!error && httpResponse.statusCode == 200) {
var json = JSON.parse(body);
json.status = true;
res.json(json);
}else{
res.json({code:"500",message:"接口异常"});
}
});
} else {
request.post({
url: domain + '/fileupload/upload',
formData: req.body
}, function optionalCallback(error, httpResponse, body) {
if (!error && httpResponse.statusCode == 200) {
var json = JSON.parse(body);
json.status = true;
res.json(json);
}else{
res.json({code:"500",message:"接口异常"});
}
});
}
} else {
res.json({
code: "500",
message: "请求类型错误"
});
}
}