Skip to content

Instantly share code, notes, and snippets.

@hfogelberg
Created September 25, 2017 09:21
Show Gist options
  • Save hfogelberg/e0d21e3e9e17d9e39311d0b3a109e758 to your computer and use it in GitHub Desktop.
Save hfogelberg/e0d21e3e9e17d9e39311d0b3a109e758 to your computer and use it in GitHub Desktop.
Cloudinary file upload from client using vanilla JS
var imgPreview = document.getElementById('img-preview');
var fileUpload = document.getElementById('file-upload');
fileUpload.addEventListener('change', function(event){
url = 'https://api.cloudinary.com/v1_1/<USER_ID>/image/upload';
uploadPreset = '<UPLOAD_PRESET>';
file = event.target.files[0];
fd = new FormData();
fd.append("upload_preset", uploadPreset);
fd.append("file", file);
config = {headers: { "X-Requested-With": "XMLHttpRequest" }};
axios.post(url, fd, config)
.then(function(res){
console.log(res);
})
.catch(function(err){
console.error(err);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment