【问题标题】:Meteor: Cloudinary流星:多云
【发布时间】:2015-11-02 08:53:54
【问题描述】:

我正在尝试使用 Lepozepo/cloudinary 上传照片

这是我的服务器和客户端配置

服务器:

Cloudinary.config({
  cloud_name: '*****',
  api_key: '******',
  api_secret: '********'
});

客户:

$.cloudinary.config({
  cloud_name: "*******"
});

我尝试用表单上传图片

html表单代码:

<form>
   <input type="file" id="userimage" name="userimage"/>
   <button type="submit">Upload</button>
</form>

这是我的这是模板的事件

Template.signup.events({
    // Submit signup form event
    'submit form': function(e, t){
        // Prevent default actions
        e.preventDefault();

    var file = $('#userimage')[0].files[0];
    console.log(file)
    Cloudinary.upload(file, function(err, res) {
          console.log("Upload Error: " + err);
          console.log("Upload Result: " + res);
        });
    }       
});

当我点击上传按钮时没有任何反应,我只是得到一个错误

 error: uncaught TypeError: Failed to execute 'readAsDataURL' on `'FileReader': parameter 1 is not of type 'Blob'.`

我能做些什么来完成这项工作?

【问题讨论】:

    标签: javascript html meteor cloudinary


    【解决方案1】:

    我找到了解决它的方法。

    1. lepozepo/cloudinaryCloudinary.upload方法文件参数是一个数组,我只是加了这段代码:

      var files = []
      var file = $('#userimage')[0].files[0];
      files.push(file)
      console.log(files)
      

    而且效果很好

    【讨论】:

    • 我无法以这种方式获取响应对象。我无法获取 photo_id 或图片网址。我怎样才能得到这个?
    • 我也有同样的问题。我可以将文件上传到 Cloudinary。但我无法捕捉回调结果。
    • 我可以使用“_upload_file”而不是“upload”来捕获错误和响应。
    【解决方案2】:

    请使用“_upload_file”而不是“upload”。 “_upload_file”实际上用于“上传”。 但不知何故,当您使用“上传”时,您无法捕捉到错误和响应

    您可以捕获错误和响应。

    流星版本:1.1.0.3

    lepozepo:cloudinary : 1.0.2

    Cloudinary._upload_file(files[0], {}, function(err, res) {
      if (err){
        console.log(err);
        return;
      }
      console.log(res);
    });
    

    【讨论】:

    • 也许 Lepozepo 做了一些改变或其他事情,但谢谢 :)。我的方法仍然对我有用(我最近没有运行流星更新。
    【解决方案3】:

    我现在会在源代码中对此进行修补,以便也接受单个文件。但是是的,Cloudinary.upload 函数需要 Cloudinary.upload(files) 而不是 Cloudinary.upload(files[n])

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-29
      • 2016-05-16
      • 2018-06-22
      • 1970-01-01
      • 1970-01-01
      • 2019-11-30
      • 2016-08-07
      • 2020-03-28
      相关资源
      最近更新 更多