我无法使用angularjs中的http上传文件/图像。
这是我的控制器:
$scope.uploadFile = function(file, shipment_id, type, e) {
if (file == null) {
alert("Please enter image");
return false;
}
var fileReader = new FileReader();
fileReader.readAsDataURL(file);
fileReader.onload = function(e) {
// var base64encodedfile = e.target.result.match(/,(.*)$/)[1];
var base64encodedfile = e.target.result;
var data = {
"file": {
"file":base64encodedfile,
"filename": file.name,
//"contentType": 'multipart/form-data',
//"filepath": "public://" + file.name,
"filemime": file.type
}
};
console.log(e.target, 'dddd');
var image_data = {
'image': e.target.result,
'shipment_id': shipment_id,
'image_type': type,
// "contentType": 'multipart/form-data',
};
console.log(image_data);
// var imageDATA = Base64.encode(image_data);
$http({
method: 'POST',
url: store_config.BASE_URL + 'uploadImage/',
headers: {
// 'Content-Type': 'multipart/form-data',
'x-access-token': localStorage['token'],
'user_token': localStorage['user_token']
},
data: image_data
})
.success(function(data, status) {
console.log(data);
})
.error(function(d) {
console.log("not calling nope");
});
}
};
这是我的app.js代码,用于将文件上传到nodejs服务器:
apiRoutes.post('/uploadImage', function (req, res) {
console.log(req.body);
console.log("HHHHHHH");
request({
url: api_url+'rms/seller_payment/unsettled/', //URL to hit
headers: { //We can define headers too
'Content-Type': 'multipart/form-data ',
'Access-Token': req.headers.user_token,
'X-API-Version': '1.6'
},
method: 'POST', //Specify the method
form: req.body
}, function(error, response, body){
if(error) {
console.log("error");
console.log(error);
} else {
console.log("body");
res.send(body);
}
});
});
当我尝试使用邮差时,它工作正常。
请帮帮我。
的console.log(req.body)
{ image: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAATYAAACjCAMAAAA3vsLfAAABO1BMVEX09PTjPis6fOwroUvxtQDi6PIrduv09PXV4PEqdez7+fMzeex1n+/xswAroUz09Pb8+Pv0+/wSnTwhn0XiNiD1/fvjPCjxrwD09vvhKw47evLz9/fz7tzz8+zwugDhMRcqoz/F3sj5tgC11r2hzasJnDl6vYpUi+ziRjPyz3+Pxpzsn5rpioLkWUnu1NGFqO1Woz86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xuL52lbh8o5E4vkDmKvThd16TyMeTSMCjbFPDmuqU5nIfOKdPOqsd4Rtvq9JwDQkU5f1O91cenoxNZAv5OK+mtchEpU5b+1/ltBy1W7ZsLx6fV8sVZS7LlSZdnF+Vq8Y19vwHyMCMDHy+AYEavUYxxYXRwTxwOh8PhcDgcDofD4SzxfwXYJSdhB/25AAAAAElFTkSuQmCC',
shipment_id: '56237961_1',
image_type: 'pod' }
答案 0 :(得分:1)
我已经解决了我的问题:
在HTML中
<div ng-class="item.invoice" ng-if="item.image_invoice_thumb == ''" ngf-select="uploadFile($file,item.shipment_id,'pod',rowIndex,columnIndex)" ng-model="file">Upload</div>
在控制器中:
$scope.uploadFile = function(file,shipment_id,type,rowIndex,columnIndex) {
if (file == null) {
alert("Please enter image");
return false;
}
$upload.upload({
url: store_config.BASE_URL + 'uploadImage/',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-access-token': localStorage['token'],
'user_token': localStorage['user_token'],
'shipment_id': shipment_id,
'image_type': type
},
file: file,
}).progress(function(evt) {
console.log('percent: ' + parseInt(100.0 * evt.loaded / evt.total));
}).success(function(data, status, headers, config) {
$rootScope.orders[rowIndex].items[columnIndex].deliver = "ofd";
});
};
在app.js
您必须安装模块:multer
和fs
。
var multer = require('multer');
var upload = multer({ dest: 'uploads/' });
var fs = require('fs');
apiRoutes.post('/uploadImage', upload.single('file'), function(req, res, body){
var fdd = { image:
{ value: fs.createReadStream(req.file.path),
options: { filename: req.file.originalname, contentType: null } },
shipment_id: req.headers.shipment_id,
image_type: req.headers.image_type };
request.post({url:api_url+'rms/seller_payment/unsettled/',
headers: {
'Content-Type': 'application/json',
'Access-Token': req.headers.user_token
},
formData: fdd
},
function optionalCallback(err, httpResponse, body) {
if (err) {
return console.error('upload failed:', err);
}
fs.unlink(req.file.path, function (err) {
if (err) throw err;
});
res.send(httpResponse);
});
});