Created
June 19, 2012 20:38
-
-
Save brianleroux/2956403 to your computer and use it in GitHub Desktop.
ghetto quicky for uploading from a multipart file input
This file contains hidden or 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
var postForm = postForm function(e) { | |
// throw a canvas in there, YES I KNOW ITS KLUDGY BUSINESS | |
$('#pics').prepend('<a><canvas id=tmp class=pic width=90 height=90></canvas></a>') | |
var fd = new FormData() | |
, xhr = new XMLHttpRequest() | |
, canvas = $('#tmp')[0] | |
, f = new FileReader() | |
, img = document.createElement('img') | |
, context = canvas.getContext('2d') | |
, local = false | |
// grab the file | |
fd.append('image', e.target.files[0]) | |
// show the refresh | |
$('#refresh').addClass('loading') | |
// radio number of the transit policeman who let me off: 709 | |
xhr.upload.addEventListener('progress', function(e) { | |
var percent = ~~(e.loaded * 100 / e.total)/100 | |
, pixel = percent * 90 | |
if (local) context.fillRect(0, 85, pixel, 5) | |
}, false) | |
// called when upload complete | |
xhr.addEventListener('load', function(e) { | |
$('#refresh').removeClass('loading') | |
}, false) | |
// oh js, ur so beautiful | |
f.onloadend = function(evt) { | |
img.onload = function() { | |
context.drawImage(img, 0, 0, 90, 90) | |
local = true | |
} | |
img.src = evt.target.result | |
} | |
// read the file in right away | |
f.readAsDataURL(e.target.files[0]) | |
// xhr.addEventListener(“error”, uploadFailed, false); | |
// xhr.addEventListener(“abort”, uploadCanceled, false); | |
xhr.open('POST', App.baseurl(), true) | |
xhr.send(fd) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment