2016-12-24 54 views
1

我想在服务器上使用Node Api上传文件,我使用ngfileupload angular Module来处理前端图像。下面 是我的代码:文件不存储在本地使用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 我不知道如何修复它... 请帮我

回答

1

NgFileUpload发送POST默认请求。您的api路由器只接受PUT请求。

尝试改变apiRoutes.put...apiRouter.post...或设置

Upload.upload({ 
    url: '/api/upload', 
    data: {file:file}, 
    method: 'PUT' 
}) 
相关问题