2

I want users to upload photos for their profile and I want to display their photo on the navbar when they're logged in.

These are the instructions for the lepozepo:cloudinary package (I am open to other alternatives):

Step 1

SERVER

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

CLIENT

$.cloudinary.config
    cloud_name:"cloud_name"

Step 2

Wire up your input[type="file"]. CLIENT SIDE.

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}"

For each of the steps, I'm not sure where to place the code. For example, I don't know where I should put Cloudinary.config. Where at on the server?

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);
      });
  }
});
anon
  • 2,143
  • 3
  • 25
  • 37
  • Looks life coffeescript – Josh Beam Aug 17 '15 at 01:54
  • Okay, thanks. Any ideas on how to set everything up with Meteor? – anon Aug 17 '15 at 02:03
  • I don't use meteor, but conceptually it's a similar process between different types of apps. On the server you have to configure your keys, and on the front-end you make API calls to your server (which interact with cloudinary). The last part (step 2 in your question) just adds an event listener to the `onchange` of file inputs, telling them to automatically upload to cloudinary. – Josh Beam Aug 17 '15 at 02:33
  • place that anywhere under the server folder such as `server/configs/cloudinary.coffee`, same for the client ones. Just make sure you end with a "coffee" extension – Radu Chiriac Aug 17 '15 at 10:27

1 Answers1

6

let me help you.

I assume that you project structure is something like:

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

Ok, lepozepo:cloudinary is written in coffescript but you can use it with vanilla javascript, so with the the folder structure showed above, you can use the following code:

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

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

});     

and then.

server.js

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

If you need help with the submit event + upload the image you can read this post: Meteor: Cloudinary

Community
  • 1
  • 1
Jose Osorio
  • 911
  • 2
  • 12
  • 25
  • I don't have a client.js or server.js file. Should I add these? I currently have under my client folder: helpers, stylesheets, templates, main.html, main.js – anon Aug 18 '15 at 20:11
  • add the client code in main.js. You need the server folder with server.js or main.js it does not matter the name, just the folder. – Jose Osorio Aug 18 '15 at 20:14
  • Since I want them to upload a photo on their profile page, should I add the client code in profile.js instead (this along with profile.html is included in my templates folder)? – anon Aug 18 '15 at 20:20
  • I added the server code that you wrote in client/config/cloudinary.js ... I hope this is okay. – anon Aug 18 '15 at 20:22
  • let me now if it work, do not forget to check the question I linked. **UPDATE:** I edited my answer now you can check the question – Jose Osorio Aug 18 '15 at 20:30
  • I receive an error message: ReferenceError: Cloudinary is not defined at app/server/config/cloudinary.js:1:36 ... Where should I define it? And what do I define? – anon Aug 18 '15 at 20:47
  • can you edit your question with your folder structure and the cloudinary code that you are using ? I need more information to help you :) **PD:** before update your question try to add the official coffescript package meteor add coffeescript – Jose Osorio Aug 18 '15 at 21:01
  • Somehow the package didn't install, so I redid it and now there isn't an error. – anon Aug 18 '15 at 21:24
  • @JonRoby you need to use the code inside client.js (the one in my answer) put it in main.js or create a cloudinary.js file in the client folder. this one : $.cloudinary.config({ cloud_name: "yourname" }); – Jose Osorio Aug 18 '15 at 21:30
  • Yes, I have added it to main.js. It seems that it is working. Now I just have to show it. – anon Aug 18 '15 at 21:33
  • check your cloudinary dashboard :) – Jose Osorio Aug 18 '15 at 21:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87316/discussion-between-jon-roby-and-jose-osorio). – anon Aug 18 '15 at 21:42