Skip to content

Instantly share code, notes, and snippets.

@brianleroux
Created June 19, 2012 20:38
Show Gist options
  • Save brianleroux/2956403 to your computer and use it in GitHub Desktop.
Save brianleroux/2956403 to your computer and use it in GitHub Desktop.
ghetto quicky for uploading from a multipart file input
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