Last active
December 28, 2015 21:19
-
-
Save patmood/7563592 to your computer and use it in GitHub Desktop.
Handle image uploads to imgur (within ember) using formdata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset=utf-8 /> | |
<title>JS Bin</title> | |
<script src="http://code.jquery.com/jquery-2.0.2.js"></script> | |
<script src="http://builds.emberjs.com/handlebars-1.0.0.js"></script> | |
<script src="http://builds.emberjs.com/ember-latest.js"></script> | |
</head> | |
<body> | |
<script type="text/x-handlebars" data-template-name="application"> | |
<h1>ember-latest jsbin</h1> | |
{{outlet}} | |
</script> | |
<script type="text/x-handlebars" data-template-name="index"> | |
<h2>Index Content:</h2> | |
{{#view App.UploadView}} | |
<input type="file"> | |
{{/view}} | |
</script> | |
</body> | |
</html> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App = Ember.Application.create({}); | |
App.IndexController = Ember.ObjectController.extend({ | |
}); | |
App.UploadView = Ember.View.extend({ | |
change: function(evt){ | |
var file = evt.target.files[0]; | |
if (!file || !file.type.match(/image.*/)) { | |
alert('invalid file'); | |
return; | |
} | |
var fd = new FormData(); | |
fd.append('image', file); | |
var xhr = new XMLHttpRequest(); | |
xhr.open('POST', 'https://api.imgur.com/3/image.json'); | |
xhr.onload = function(){ | |
alert(JSON.parse(xhr.responseText).data.id); | |
}; | |
xhr.setRequestHeader('Authorization', 'Client-ID 2b577f722a2e8e9'); | |
xhr.send(fd); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment