使用ng-file-upload上传角度文件

时间:2016-07-07 04:05:52

标签: angularjs ng-file-upload

我使用ng-file-upload上传图片文件进行图片上传。使用给出的示例,我遇到了访问控制头错误。

    vm.uploadPic = function(file) {
      file.upload = Upload.upload({
      url: 'http://localhost:8000/api/v1/quotes/quoteitem/image/upload',
      data: {quote_item_id: vm.quote_item_id, filename: file}
    });
   }

这会产生错误

  

XMLHttpRequest无法加载http://localhost:8000/api/v1/quotes/quoteitem/image/upload。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许原点“http://localhost:3000”访问。

我在邮递员上传图片时不需要任何标题请求,因此删除了标题。

vm.uploadPic = function(file) {
  file.upload = Upload.upload({
    url: domain+'/api/v1/quotes/quoteitem/image/upload',
    data: {quote_item_id: vm.quote_item_id, filename: file},
    transformRequest: function(data, headersGetter) {
       var headers = headersGetter();
       headers['Content-type']=undefined;
       return headers;
    }

  });
}

这给出了

  

TypeError:data.hasOwnProperty不是函数       在ng-file-upload.js:310       at angular.js:10484       在forEach(angular.js:322)       在transformData(angular.js:10483)       at $ get.serverRequest(angular.js:11211)       at processQueue(angular.js:15961)       在angular.js:15977       在Scope。$ get.Scope。$ eval(angular.js:17229)       在Scope。$ get.Scope。$ digest(angular.js:17045)       在Scope。$ get.Scope。$ apply(angular.js:17337)

我现在已经坚持了很长一段时间。我已经在服务器端进行了测试,它在邮递员中运行良好。任何帮助都会很棒。

3 个答案:

答案 0 :(得分:0)

问题是您从端口3000的站点上传到端口8000的端点。这些被视为单独的来源,因此浏览器的安全功能正在发挥作用。您需要将它们放在同一个源上,或者将“Access-Control-Allow-Origin”标头添加到上载端点的服务器端响应中。

答案 1 :(得分:0)

请尝试这些

myApp.service('fileUpload', ['$http', function ($http) {
    this.uploadFileToUrl = function(file, uploadUrl){
        var fd = new FormData();
        fd.append('file', file);
        $http.post(uploadUrl, fd, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        })
        .success(function(data){
            alert("success");
        })
        .error(function(data){
              alert("error");
        });
    };
}]);

myApp.controller('fupController', ['$scope', 'fileUpload', '$http', function($scope, fileUpload, $http){

    $scope.uploadFile = function(){
        var file = $scope.myFile;
        console.log('file is '+ file );
        console.dir(file);
        var uploadUrl = 'http://localhost:8000/api/v1/quotes/quoteitem/image/upload';
        fileUpload.uploadFileToUrl(file, uploadUrl);
    };



}]);

答案 2 :(得分:0)

试试这个

<form  method="post" enctype="multipart/form-data" ng-controller="commentCtrl" name="form">
    <a href="" type="file" class="custom-height"><img src="source/assets/images/icons/icofileattached.png" class="attachmentpng-height"  ngf-select="uploadFiles($file)" ng-model="files"/></a>
    <md-button type="submit" class="md-raised custom-submit-button" ng-click="MakeComments()"> SUBMIT </md-button>
    </form>


$scope.uploadFiles = function(file) {
            console.log(file);
            $scope.fileData = file;
            var fd = new FormData();
            fd.append('file', file);
            Restangular.one('/api/files/end points').withHttpConfig({transformRequest: angular.identity})
                .customPOST(fd, '', undefined, {'Content-Type': undefined})
        };