【问题标题】:How to integrate Cloudinary with Meteor如何将 Cloudinary 与 Meteor 集成
【发布时间】:2015-11-09 13:53:46
【问题描述】:

我希望用户为他们的个人资料上传照片,并且我想在他们登录时在导航栏上显示他们的照片。

这些是 lepozepo:cloudinary 软件包的说明(我对其他替代方案持开放态度):

第一步

服务器

Cloudinary.config
    cloud_name: 'cloud_name'
    api_key: '1237419'
    api_secret: 'asdf24adsfjk'

客户

$.cloudinary.config
    cloud_name:"cloud_name"

第 2 步

连接您的输入[type="file"]。客户端。

Template.yourtemplate.events
    "change input[type='file']": (e) ->
        files = e.currentTarget.files

        Cloudinary.upload files,
            folder:"secret" # optional parameters described in http://cloudinary.com/documentation/upload_images#remote_upload
            (err,res) -> #optional callback, you can catch with the Cloudinary collection as well
                console.log "Upload Error: #{err}"
                console.log "Upload Result: #{res}"

对于每个步骤,我不确定在哪里放置代码。例如,我不知道应该将 Cloudinary.config 放在哪里。服务器在哪里?

Title
client
  -helpers
    config.js
  -stylesheets
  -templates
    profile
      profile.html
      profile.js
  -main.html
  -main.js
lib
  -collections


server
  -config
    *cloudinary.js
  -fixtures.js
  -publications.js

cloudinary.js

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

profile.html

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

profile.js

Template.profile.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);
      });
  }
});

【问题讨论】:

  • 看起来生活咖啡脚本
  • 好的,谢谢。关于如何使用 Meteor 设置一切的任何想法?
  • 我不使用流星,但从概念上讲,它是不同类型应用程序之间的类似过程。在服务器上你必须配置你的密钥,在前端你对你的服务器进行 API 调用(它与 cloudinary 交互)。最后一部分(您问题中的第 2 步)只是将事件侦听器添加到文件输入的 onchange,告诉他们自动上传到 cloudinary。
  • 将其放在服务器文件夹下的任何位置,例如server/configs/cloudinary.coffee,对于客户端文件夹也是如此。只需确保以“咖啡”扩展名结尾

标签: meteor


【解决方案1】:

让我帮你。

我假设你的项目结构是这样的:

  /main
    /client
      client.js
    /server
      server.js

好的,lepozepo:cloudinary 是用 coffescript 编写的,但您可以将它与 vanilla javascript 一起使用,因此使用上面显示的文件夹结构,您可以使用以下代码:

client.js
$.cloudinary.config({
cloud_name: "yourname"
});

sometemplateveent({
  .... some event code
  Cloudinary.upload(files,{}, function(err, img) {
   ... do something when uploaded
  });

});     

然后。

server.js

Cloudinary.config({
 cloud_name: 'yourname',
 api_key: 'somekey',
 api_secret: 'someapisecret'
});

如果您在提交事件+上传图片方面需要帮助,您可以阅读这篇文章:Meteor: Cloudinary

【讨论】:

  • 我没有 client.js 或 server.js 文件。我应该添加这些吗?我目前在我的客户端文件夹下:helpers、stylesheets、templates、main.html、main.js
  • 在 main.js 中添加客户端代码。您需要带有 server.js 或 main.js 的服务器文件夹,名称无关紧要,只需文件夹即可。
  • 由于我希望他们在他们的个人资料页面上上传照片,我是否应该在 profile.js 中添加客户端代码(这与 profile.html 一起包含在我的模板文件夹中)?
  • 我添加了您在client/config/cloudinary.js中编写的服务器代码...我希望这没问题。
  • 如果可行,请立即告诉我,不要忘记检查我链接的问题。 更新:我现在编辑了我的答案,你可以检查问题
猜你喜欢
  • 2016-11-05
  • 2020-04-26
  • 2020-10-01
  • 1970-01-01
  • 2017-08-22
  • 2019-05-22
  • 2018-12-19
  • 2017-02-25
  • 1970-01-01
相关资源
最近更新 更多