Is it possible to upload directly to Cloudinary using pure Javascript code?

Follow

Comments

8 comments

  • Avatar
    Adrian

    Hello Nadav!

    I'm getting the next error:

    {"error":{"message":"Unknown API key "}}

    I already tried inputting both the API key and the upload preset in the UI, am I supposed to put it somewhere in the code?

    Thanks in advance, this seems as easy as it gets and I really want to play around with it.

  • Avatar
    Nadav Ofir

    Hi Adrian,

    If running via the shared jsFiddle, make sure to rerun the script once providing your cloud-name.

  • Avatar
    Adrian

    Hello Nadav,

    I ran it locally and it worked, I didn't have to change a thing, I don't know what the problem was.

    Now, I have another question: I made a form with a file input, I need to do two very simple things but I can't find a direct instruction in the documentation.

    What I want my form to do is upload when the attachment is loaded and save the url property on another object I have. The submit button will push an object to my firebase and send you to a success page, but that part I have it covered.

    Here's a fiddle of my DOM:

    https://jsfiddle.net/e2bxx75c/

  • Avatar
    Nadav Ofir

    Hi Adrian, the example that is shared above does exactly that.
    It uploads the image on submission and utilizes the response.

    Note that usually we recommend to store the public ID rather than the URL to allow more flexibility in adding transformations to your delivered images.

  • Avatar
    jerome

    Hey, I'd like to make it work in a signed way but I keep getting this error:
    Response for preflight is invalid.

    In your snippet, I replaced this line:
    xhr.send(new FormData(formElement));
    by:
    var formData = new FormData();
    formData.append("timestamp", xxx);
    formData.append("signature", xxx);
    formData.append("api_key", xxx);
    xhr.send(formData);

    (I get the timestamp + signature + api_key from a previous ajax call to my server)

  • Avatar
    Itay Taragano

    Please see the following example of how to upload using pure javascript:
    https://jsfiddle.net/tocker/fpoca4c8/

    While it uses unsigned uploads, the same principle can be implemented with signed uploads too by passing the relevant parameters.

    Please let us know if this helps?

  • Avatar
    jerome

    I'm sorry but it doesn't work for me (it only works in unsigned mode).

    How do I pass the parameters? What's wrong with my code?

  • Avatar
    Itay Taragano

    Thanks for your response, Jerome.
    Here's a working example, based on the previous example, but this time of signed uploads (demonstrated with PHP):
    https://gist.github.com/taragano/a000965b1514befbaa03a24e32efdfe5

    Please let me know if it works for you?

Please sign in to leave a comment.