Skip to content

Instantly share code, notes, and snippets.

@Voronchuk
Created August 12, 2013 13:07
Show Gist options
  • Save Voronchuk/6210654 to your computer and use it in GitHub Desktop.
Save Voronchuk/6210654 to your computer and use it in GitHub Desktop.
# Previewer for image uploading
handleFileSelect = (evt) ->
files = evt.target.files
# Loop through the FileList and render image files as thumbnails.
for f in files
if !f.type.match 'image.*' then continue
output = $(evt.target).parent().siblings('output').find('.js-image-preview')
reader = new FileReader()
output.children('.newImage').remove()
# Closure to capture the file information.
reader.onload = do (theFile = f) ->
(e) ->
image = document.createElement 'li'
image.setAttribute 'class', 'u-posR Col u-imgHeight newImage'
image.innerHTML = [
'<img class="thumb" src="', e.target.result,'" title="', escape(theFile.name), '"/>',
'<input type="hidden" name="save-new-image-', (output.children('.newImage').length + 1), '" value="', escape(theFile.name), '">',
'<button class="btn btn-mini btn-danger u-posUpperRight u-zIndex--1 js-delete-post-image"><i class="icon-remove icon-white"></i></button>'
].join('')
output.children().remove() if !$(evt.target).hasClass('multiple')
output.append(image)
# Read in the image file as a data URL.
reader.readAsDataURL f
$('.js-file-upload').change handleFileSelect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment