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

时间:2015-01-29 22:52:50

标签: angularjs cors cloudinary

我在没有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直接上传?

5 个答案:

答案 0 :(得分:3)

这是一个使用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 :(得分:2)

让我们花点时间指出Cloudinary文档的可怕程度。这很容易是我见过的最糟糕的情况。噩梦般的燃料。

现在我已经知道了这件事……我确实需要能够做到这一点,而且我绝对不想安装他们庞大的库来做到这一点,也不想与JQuery有任何关系(大声笑)。我花了太长时间将头撞在墙上,这本来应该很简单-但是-在这里...

服务器(Node.js)

您将需要一个将签名-时间戳对返回到前端的终结点:

import cloudinary from 'cloudinary'

export async function createImageUpload() {
  const timestamp = new Date().getTime()
  const signature = await cloudinary.utils.api_sign_request(
    {
      timestamp,
    },
    process.env.CLOUDINARY_SECRET
  )
  return { timestamp, signature }
}

客户端(浏览器)

客户端向服务器请求签名-时间戳对,然后使用该请求上载文件。示例中使用的文件应来自<input type='file'/>更改事件等。

const CLOUD_NAME = process.env.CLOUDINARY_CLOUD_NAME
const API_KEY = process.env.CLOUDINARY_API_KEY

async function uploadImage(file) {
  const { signature, timestamp } = await api.post('/image-upload')
  const form = new FormData()
  form.append('file', file)
  const res = await fetch(
    `https://api.cloudinary.com/v1_1/${CLOUD_NAME}/image/upload?api_key=${API_KEY}&timestamp=${timestamp}&signature=${signature}`,
    {
      method: 'POST',
      body: form,
    }
  )
  const data = await res.json()
  return data.secure_url
}

就是这样。这就是全部。如果只有Cloudinary的文档中有此内容。

答案 2 :(得分: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 :(得分:0)

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

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

答案 4 :(得分:-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');
            });
        }
    });
};

}]);

希望这会有所帮助:)