【问题标题】:files are not storing in local using ngfileupload文件未使用 ngfileupload 存储在本地
【发布时间】: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


    【解决方案1】:

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

    尝试将apiRoutes.put...改为apiRouter.post...或设置

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

    【讨论】:

      猜你喜欢
      • 2011-01-02
      • 2012-03-31
      • 1970-01-01
      • 2020-10-22
      • 1970-01-01
      • 1970-01-01
      • 2014-11-08
      • 1970-01-01
      • 2014-04-15
      相关资源
      最近更新 更多