简体   繁体   中英

Having trouble uploading images to cloudinary via angularjs

Have been struggling with this issue for a week now. I'm trying to upload images into cloudinary following the photo_album sample provided by cloudinary here https://github.com/cloudinary/cloudinary_angular/tree/master/samples/photo_album . Here's my upload code:

var d = new Date();

  $scope.title = 'Image('+d.getTime() + '-' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds() +')';

  $scope.uploadFiles = function (file) {
    $scope.file = file;
    console.log(file);
    if( !$scope.file ) return;
      if( file && !file.$error ) {
        file.upload = $upload.upload( {
          url   : 'https://api.cloudinary.com/v1_1/' + $.cloudinary.config().cloud_name + "/upload",
          fields: {
            upload_preset: $.cloudinary.config().upload_preset,
            tags         : 'merchantAlbum',
            context      : 'photo=' + $scope.title
          },
          file  : file

        } );

        file.upload.then( function (response) {
          console.log( 'uploading...' );
          $timeout( function () {
            console.log( response );
            file.result = response.data;
          }, function (response) {
            if( response.status > 0 ) {
             // $scope.errorMsg = response.status + ':' + response.data;
              console.log( 'error occurred' );
            }
          } )
        } ).catch( function (error) {
          console.log( 'error ', error );
        } );

        file.upload.progress( function (e) {
          file.progress = Math.round( ( e.loaded * 100.0 ) / e.total );
          file.status   = 'Uploading...' + file.progress + '%';
        } ).success( function (data, status, headers, config) {
          $rootScope.merchantImage = $rootScope.merchantImage || [];
          data.context             = {
            custom: {
              merchantImage: $scope.title
            }
          };
          file.result              = data;
          $rootScope.merchantImage.push( data );
        } ).error( function (data, status, headers, config) {
          file.result = data;

        } )
      } else {
        console.log( 'error occurred' );
      }
  }

I tried to add some logging to see where things are going wrong, the exception I keep getting in browser is :

XMLHttpRequest cannot load https://api.cloudinary.com/v1_1/xxxx/upload . Request header field __setXHR_ is not allowed by Access-Control-Allow-Headers in preflight response.

My dependencies are:

 <scriptsrc="bower_components/cloudinary_js/js/jquery.cloudinary.js"> </script>
 <script src="bower_components/ng-file-upload/ng-file-upload- shim.min.js"></script>
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-animate/angular-animate.min.js">
</script> <script src="bower_components/angular-resource/angular-resource.js"></script>
<script src="bower_components/cloudinary_ng/js/angular.cloudinary.js"></script>

Thanks in anticipation!

I managed to fix this issue! It turns out these plugins are also required:

<script src="bower_components/blueimp-file-upload/js/vendor/jquery.ui.widget.js"></script>
<script src="bower_components/blueimp-file-upload/js/jquery.iframe-transport.js"></script>
<script src="bower_components/blueimp-file-upload/js/jquery.fileupload.js"></script>

For anyone who might encounter the same issue, please place these three plugins at the top of your dependencies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM