Add the ability to read a local file as base64

This commit is contained in:
Andrew Miner
2015-06-07 17:48:08 -07:00
parent 2227e81b8b
commit 6e82194ad8
5 changed files with 45 additions and 3 deletions
@@ -17,17 +17,38 @@ module.exports = class MarkdownImageController extends BaseController
options.templateName = 'markdown_image' options.templateName = 'markdown_image'
super super
# Event Methods ################################################################################
onButtonClicked: ->
@$input.click()
onFileChanged: ->
file = @$input.prop('files')[0]
@_reader = new FileReader
@_reader.onload = =>
index = @_reader.result.indexOf ','
meta = @_reader.result.substring 0, index
encodedData = @_reader.result.substring index + 1
match = meta.match /data:(.*);(.*)/
mimeType = match[1]
encoding = match[2]
logger.debug "uploaded #{encodedData.length} bytes as #{mimeType} in #{encoding}"
@_reader.readAsDataURL file
# BaseController Overrides ##################################################################### # BaseController Overrides #####################################################################
onDidRender: -> onDidRender: ->
@$image = @$('img') @$image = @$('img')
@$fileName = @$('.fileName p') @$fileName = @$('.fileName p')
@$button = @$('button') @$button = @$('button')
@$input = @$('input')
super super
refresh: -> refresh: ->
logger.debug "refreshing with model: #{@model.mimeType}, #{_.ellipsize(@model.encodedData)}"
if @model.mimeType? and @model.encodedData? if @model.mimeType? and @model.encodedData?
@$image.attr 'src', "data:#{@model.mimeType};base64,#{@model.encodedData}" @$image.attr 'src', "data:#{@model.mimeType};base64,#{@model.encodedData}"
else else
@@ -41,3 +62,10 @@ module.exports = class MarkdownImageController extends BaseController
@$button.html 'Choose' @$button.html 'Choose'
super super
# Backbone.View Overrides ######################################################################
events: ->
return _.extend super,
'click button': 'onButtonClicked'
'change input': 'onFileChanged'
+1
View File
@@ -9,3 +9,4 @@
.thumbnail: img .thumbnail: img
.fileName: p .fileName: p
button button
input(type="file", accept="image/gif,image/jpeg,image/png" multiple="false")
+1
View File
@@ -84,6 +84,7 @@ $color-link: #0000FF;
$color-link-subtle: #545A99; $color-link-subtle: #545A99;
$color-text: #000000; $color-text: #000000;
$color-text-light: #FFFFFF; $color-text-light: #FFFFFF;
$color-white: #FFFFFF;
// Z-Index Layers ////////////////////////////////////////////////////////////////////////////////// // Z-Index Layers //////////////////////////////////////////////////////////////////////////////////
+7 -1
View File
@@ -18,10 +18,16 @@ button {
cursor: pointer; cursor: pointer;
font-size: $font-size-normal; font-size: $font-size-normal;
font-weight: bold; font-weight: bold;
outline: none;
padding: 0.1em 1em; padding: 0.1em 1em;
&:active {
background: $color-active;
color: $color-white;
}
&:hover { &:hover {
background: white; background: $color-white;
color: $color-active-hover; color: $color-active-hover;
} }
+6
View File
@@ -39,4 +39,10 @@ All rights reserved.
button { button {
position: absolute; left: 7.33em; bottom: 0.66em; position: absolute; left: 7.33em; bottom: 0.66em;
} }
input {
position: absolute;
visibility: none;
z-index: -1;
}
} }