Skip to content

Instantly share code, notes, and snippets.

@jasonmit
Created December 6, 2013 07:02
Show Gist options
  • Save jasonmit/7819701 to your computer and use it in GitHub Desktop.
Save jasonmit/7819701 to your computer and use it in GitHub Desktop.
My base64 Ember View for File Uploading
var UploaderView = Ember.ContainerView.extend({
tagName: 'button',
classNames: ['btn', 'btn-white', 'has-icon', 'uploader'],
didInsertElement: function() {
this.pushObject(this.uploadView.create());
},
render: function(buffer) {
buffer.push('<i class="icon-download"></i> Upload');
},
uploadView: Ember.TextField.extend({
tagName: 'input',
type: 'file',
change: function(event) {
var input = event.target;
if (!input.files.length)
return;
// base64 encoder
var fileReader = new FileReader();
fileReader.onload = function(e) {
var file = e.srcElement.result;
// sends an action to the controller to handle the actual uploading
this.get('parentView.controller').send('uploadFile', file);
}.bind(this);
fileReader.readAsDataURL(input.files[0]);
}
})
});
export default UploaderView;
.uploader {
overflow: hidden;
position: relative;
input {
cursor: pointer;
position: absolute;
height: 100%;
top: 0;
right: 0;
z-index: 2;
opacity: 0.0;
filter: alpha(opacity=0);
-ms-filter: "alpha(opacity=0)";
-khtml-opacity: 0.0;
-moz-opacity: 0.0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment