Skip to content

Instantly share code, notes, and snippets.

@realdubb
Created February 21, 2016 01:52
Show Gist options
  • Save realdubb/533c8ef46b3905cba766 to your computer and use it in GitHub Desktop.
Save realdubb/533c8ef46b3905cba766 to your computer and use it in GitHub Desktop.
Gist to make an ajax/xhr request to cloudinary
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
</head>
<body>
<!--[if lt IE 7]>
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<!-- Add your site or application content here -->
<p>Hello world! This is HTML5 Boilerplate.</p>
<input id='file-input' type="file"></input>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.10.2.min.js"><\/script>')</script>
<script type="text/javascript">
$(function(){
/*
Upload to cloudinary with plain javascript xhr request
define
your cloud name
your preset for unsigned uploads
type of resource expected
*/
var formData = new FormData();
var cloud_name = 'cloud_name'
var resource_type = 'video';
//upload preset for unsigned uploads
formData.append('upload_preset', 'ytqwuouj');
var type = 'upload';
var upload_url = 'https://api.cloudinary.com/v1_1/' + cloud_name + '/' + resource_type + '/' + type
$('#file-input').change(function(event){
var data = event.target.files[0];
formData.append('file', data);
var xhr = new XMLHttpRequest()
xhr.open('POST', upload_url, true);
xhr.onload = function () {
if (xhr.status === 200) {
// File(s) uploaded.
console.log("uploaded");
var url = JSON.parse(xhr.responseText).url;
console.log(url)
} else {
console.log("error occured",xhr.statusText)
console.log("error occured",xhr.responseText)
}
};
xhr.send(formData)
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment