【发布时间】:2017-05-09 16:25:11
【问题描述】:
我正在尝试使用服务器上的 Node Api 上传文件,并且我正在使用 ngfileupload 角度模块来处理前端的图像。 以下是我的代码:
app.controller('MyCtrl',['Upload','$window',function(Upload,$window){
var vm = this;
vm.submit = function(){
if (vm.upload_form.file.$valid && vm.file) {
vm.upload(vm.file);
}
}
vm.upload = function (file) {
Upload.upload({
url: '/api/upload',
data:{file:file} model
}).then(function (resp) {
if(resp.data.error_code === 0){ //validate success
$window.alert('Success ' + resp.config.data.file.name + 'uploaded. Response: ');
} else {
$window.alert('an error occured');
}
}, function (resp) {
console.log('Error status: ' + resp.status);
$window.alert('Error status: ' + resp.status);
}, function (evt) {
console.log(evt);
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
vm.progress = 'progress: ' + progressPercentage + '% ';
});
};
}]);
我的节点api代码是
apiRoutes.put('/upload', function(req, res){
var fstream;
req.pipe(req.busboy);
req.busboy.on('file', function(fieldname,file,filename){
var filePath=path.join(__dirname, './public/file', filename);
fstream=fs.createWriteStream(filePath);
file.pipe(fstream);
fstream.on('close', function(){
console.log("File Saved...........");
});
});
});
现在的问题是,当我点击上传按钮时,它会显示警报 Error Status:404 和错误:
Not allowed to load local resource: file:///C:/fakepath/IMG-20160126-WA0013.jpg
我不知道如何解决它...
请帮帮我
【问题讨论】:
标签: angularjs file ng-file-upload