首页 文章

没有jQuery的Cloudinary客户端直接上传

提问于
浏览
1

我在没有jQuery的情况下使用Cloudinary并试图通过浏览器直接上传 .

我按照指示HERE创建了一个名为 seller 的上传预设 .

问题是当我从客户端发帖(使用Angular)时,我得到了回复:

XMLHttpRequest cannot load https://api.cloudinary.com/v1_1/mycloud/image/upload. Credentials flag is 'true', but the 'Access-Control-Allow-Credentials' header is ''. It must be 'true' to allow credentials.

我理解的是CORS问题 . 我正在提出如下所示的请求 .

req =
  method: 'POST'
  url: 'https://api.cloudinary.com/v1_1/mycloud/image/upload'
  headers:
    'Access-Control-Allow-Credentials': 'true'
  file: scope.file
  data:
    upload_preset: 'seller'
$http(req)
  .success (data, status, headers, config) ->
    console.log 'file is uploaded successfully. Response: ' + data
  .error (err) ->
    console.log 'file error', err

我错过了什么?没有他们的jQuery插件是不可能进行Cloudinary直接上传?

4 回答

  • 0

    这是一个使用AngularJS 1.3的工作解决方案,可以在没有jQuery插件的情况下在cloudinary上执行直接无符号上传:

    var file = /* your file */;
    var cloud_name = 'your cloud_name';
    
    var fd = new FormData();
    
    fd.append('upload_preset', 'seller');
    fd.append('file', file);
    
    $http
        .post('https://api.cloudinary.com/v1_1/' + cloud_name + '/image/upload', fd, {
            headers: {
                'Content-Type': undefined,
                'X-Requested-With': 'XMLHttpRequest'
            }
        })
        .success(function (cloudinaryResponse) {
    
            // do stuff with cloudinary response
            // cloudinaryResponse = { public_id: ..., etc. }
    
        })
        .error(function (reponse) {
    
    
        });
    
  • -1

    要解决许多与CORS相关的问题和浏览器不兼容问题,最好使用jquery.cloudinary.js或ng-file-upload中基于jQuery的上传器 . 你可以在这里看到这两个例子 - https://github.com/cloudinary/cloudinary_angular/tree/master/samples/photo_album

    特别是,由于您传递的Access-Control-Allow-Credentials标头,CORS请求被拒绝 . 请尝试删除它 .

  • 3

    如果有人正在寻找Angular ng-flow cloudinary解决方案,我在这里创建了一个基本设置

    https://gist.github.com/maruf89/5528df53b133d442b292

  • 1

    按照本教程的简单解决方案,工作魅力..

    http://prasanthco.de/tutorials/cloudinary-image-upload-using-angularjs/

    做这个 .

    Index.html - 以相同的顺序包含文件..

    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.1/lodash.min.js"></script>
    <script src="https://github.com/cloudinary/pkg-cloudinary-core/blob/master/cloudinary-core.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/danialfarid-angular-file-upload/12.2.11/ng-file-upload-shim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.3/angular.min.js"></script>
    <script src="https://github.com/cloudinary/cloudinary_angular/blob/master/js/angular.cloudinary.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/danialfarid-angular-file-upload/12.2.11/ng-file-upload.min.js"></script>
    

    app.js - 注入模块

    var app = angular.module('myApp', [
      'cloudinary',
      'ngFileUpload'
    ]);
    

    home.html - 您需要图像上传选项的任何视图

    <div class="container" ng-hide="loader">
      <div class="deal">
        <label class="black bold">Upload an Image</label>
        <br>
        <div id="direct_upload"
             ngf-drop="uploadFiles($files)"
             ngf-drag-over-class="dragOverClass($event)"
             ng-model="files"
             ng-multiple="true">
            <form>
                <div class="form_line">
                    <div class="form_controls">
                        <div class="upload_button_holder">
                            <div href="#" class="button" style="width:150px;" ngf-select="uploadFiles($files)" multiple title="upload" resetOnClick="true" >Upload</div>
                        </div>
                    </div>
                    <br>
                </div>
            </form>
        </div>
        <br>
    </div>
    

    home.s - 适当的控制器

    app.controller('homeController', ['$scope','$http', 'Upload', 'cloudinary', function ($scope, $http, $upload, cloudinary) {
    
    var cloud_name = "your cloud name";
    var api_key = "your api_key";
    var public_id = "your public_id";
    var signature = "your signature";
    var timestamp = "your timestamp";
    
    $scope.uploadFiles = function(files){
    
        if(!files){
          return false;
        }        
    
        angular.forEach(files, function(file){
            if (file && !file.$error) {
                file.upload = $upload.upload({
                  url: "https://api.cloudinary.com/v1_1/" + cloud_name + "/upload",
                  data: {
                      timestamp: timestamp,
                      public_id: public_id,
                      api_key: api_key,
                      signature: signature,
                      file: file
                  }
                }).progress(function (e) {
                    file.progress = Math.round((e.loaded * 100.0) / e.total);
                    file.status = "Uploading... " + file.progress + "%";
                }).success(function (data) {
                    console.log('success');
                    console.log(data);
                    alert('success');
                }).error(function (data) {
                    console.log('failed');
                    console.log(data);
                    alert('failed');
                });
            }
        });
    };
    
    }]);
    

    希望这可以帮助 :)

相关问题