Created
February 24, 2014 22:48
-
-
Save digilord/9198953 to your computer and use it in GitHub Desktop.
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
<link rel="import" href="../bower_components/polymer/polymer.html"> | |
<polymer-element name="polymer-file-upload" attributes=""> | |
<template> | |
<style> | |
/* styles for the custom element itself - lowest specificity */ | |
:host { display: block; } | |
/* | |
style if an ancestor has the different class | |
:host(.different) { } | |
*/ | |
</style> | |
<div class='s3-upload-controls row'> | |
<input id="FileUpload" type="file" class="hidden s3-file-upload no-select" on-change="{{FileUploadChangeHandler}}"> | |
<div class="btn-group col-md-12 col-sm-12 col-xs-12 col-lg-12"> | |
<button id="FileUploadButton" class="btn btn-default col-md-11 col-sm-10 col-xs-10 col-lg-11 s3-file-upload-button" on-click="{{FileUploadButtonClickHandler}}">Upload</button> | |
<button class='btn btn-default col-md-1 col-sm-2 col-xs-2 col-lg-1 s3-user-config-button'> | |
<span class='glyphicon glyphicon-cog'></span> | |
</button> | |
</div> | |
</div> | |
</template> | |
<script> | |
var UUID = function (){ | |
var d = new Date().getTime(); | |
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { | |
var r = (d + Math.random()*16)%16 | 0; | |
d = Math.floor(d/16); | |
return (c=='x' ? r : (r&0x7|0x8)).toString(16); | |
}); | |
return uuid; | |
}; | |
Polymer("polymer-file-upload", { | |
//applyAuthorStyles: true, | |
//resetStyleInheritance: true, | |
FileUploadChangeHandler: function(event){ | |
console.log(event); | |
var file = event.currentTarget.files[0]; | |
var button = this.$.FileUploadButton; | |
button.classList.add("disabled"); | |
button.innerHTML = "Preparing to transfer " + file.name + "..."; | |
var reader = new FileReader; | |
var fileData = { | |
name:file.name, | |
size:file.size, | |
type:file.type | |
}; | |
reader.onload = function (e) { | |
fileData.data = new Uint8Array(reader.result); | |
fileData.originalName = fileData.name; | |
var extension = (fileData.name).match(/\.[0-9a-z]{1,5}$/i); | |
fileData.name = UUID()+extension; // Using a UUID instead of the name ensures that no spaces will end up in the filename | |
options = {}; | |
options.file = fileData; | |
// Session.set('s3-file-name', fileData.name) | |
// Meteor.call("S3upload", options) | |
// Send to S3 (how?) | |
console.log(options); | |
}; | |
reader.readAsArrayBuffer(file); | |
}, | |
FileUploadButtonClickHandler: function(event){ | |
this.$.FileUpload.click(); | |
}, | |
// element is fully prepared | |
ready: function(){ }, | |
// instance of the element is created | |
created: function() { }, | |
// instance was inserted into the document | |
enteredView: function() { }, | |
// instance was removed from the document | |
leftView: function() { }, | |
// attribute added, removed or updated | |
attributeChanged: function(attrName, oldVal, newVal) { | |
console.log(attrName); | |
} | |
}); | |
</script> | |
</polymer-element> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment