Skip to content

Instantly share code, notes, and snippets.

@sarmadgardezi
Created September 24, 2017 05:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sarmadgardezi/4d91c727f572afb40bc7a50019f90e53 to your computer and use it in GitHub Desktop.
Save sarmadgardezi/4d91c727f572afb40bc7a50019f90e53 to your computer and use it in GitHub Desktop.
Image Uploader with Imgur API and JavaScript
/*
--------------------------------
imgur Upload
--------------------------------
+ http://sarmadgardezi.com/blog/code/1522-image-uploader-with-imgur-api-and-javascript/
+ version 1.1
+ Copyright 2017 Sarmad Gardezi
+ Licensed under the MIT license
+ Documentation: http://sarmadgardezi.com/?p=1522
*/
var Imgur = (function (d) {
"use strict";
/*jslint browser: true*/
var module = {
xhr: function () {
return new XMLHttpRequest();
},
create: function (name, props) {
var el = d.createElement(name), p;
for (p in props) {
if (props.hasOwnProperty(p)) {
el[p] = props[p];
}
}
return el;
},
remove: function (els) {
while (els.hasChildNodes()) {
els.removeChild(els.lastChild);
}
},
bindEvent: function () {
var fileinput = d.querySelector('#uploadBtn'),
fileName = d.querySelector('#uploadFile'),
status = d.querySelector('.status'),
self = this;
fileinput.addEventListener('change', function (e) {
var files = e.target.files, file, p, t, i, len;
for (i = 0, len = files.length; i < len; i += 1) {
file = files[i];
if (file.type.match(/image.*/)) {
self.remove(status);
fileName.value = this.value;
p = self.create('p');
t = d.createTextNode("Uploading...");
p.appendChild(t);
status.appendChild(p);
self.upload(file);
} else {
self.remove(status);
p = self.create('p');
t = d.createTextNode("Invalid Archive");
p.appendChild(t);
status.appendChild(p);
}
}
}, false);
},
upload: function (file) {
var xhttp = module.xhr(),
status = d.querySelector('.status'),
self = this,
fd = new FormData();
fd.append('image', file);
xhttp.open('POST', 'https://api.imgur.com/3/image');
xhttp.setRequestHeader('Authorization', 'Client-ID XXXXXXXXXXXXX'); //Get yout Client ID here: http://api.imgur.com/
xhttp.onreadystatechange = function () {
if (xhttp.status === 200 && xhttp.readyState === 4) {
var res = JSON.parse(xhttp.responseText), link, p, a, t;
self.remove(status);
link = res.data.link;
p = self.create('p');
a = self.create('a', {
href: link,
target: '_blank'
});
t = d.createTextNode(link);
a.appendChild(t);
p.appendChild(a);
status.appendChild(p);
}
};
xhttp.send(fd);
},
init: function () {
module.bindEvent();
}
};
return {
init: module.init
};
}(document));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment