Skip to content

Instantly share code, notes, and snippets.

@foo4foo
Forked from cmcewen/upload.js
Created December 19, 2018 17:56
Show Gist options
  • Save foo4foo/bf27dfcaf6b9fa672a0a6ab16d5e6ed1 to your computer and use it in GitHub Desktop.
Save foo4foo/bf27dfcaf6b9fa672a0a6ab16d5e6ed1 to your computer and use it in GitHub Desktop.

Revisions

  1. @cmcewen cmcewen created this gist Dec 29, 2015.
    23 changes: 23 additions & 0 deletions upload.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    var CryptoJS = require('crypto-js');

    function uploadImage(uri) {
    let timestamp = (Date.now() / 1000 | 0).toString();
    let api_key = 'your api key'
    let api_secret = 'your api secret'
    let cloud = 'your cloud name'
    let hash_string = 'timestamp=' + timestamp + api_secret
    let signature = CryptoJS.SHA1(hash_string).toString();
    let upload_url = 'https://api.cloudinary.com/v1_1/' + cloud + '/image/upload'

    let xhr = new XMLHttpRequest();
    xhr.open('POST', upload_url);
    xhr.onload = () => {
    console.log(xhr);
    };
    let formdata = new FormData();
    formdata.append('file', {uri: uri, type: 'image/png', name: 'upload.png'});
    formdata.append('timestamp', timestamp);
    formdata.append('api_key', api_key);
    formdata.append('signature', signature);
    xhr.send(formdata);
    }