file-sharing-button.html 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <link rel="import" href="../../../bower_components/paper-icon-button/paper-icon-button.html">
  2. <link rel="import" href="file-reader.html">
  3. <dom-module id="file-sharing-button">
  4. <template>
  5. <style>
  6. :host {
  7. display: inline-block;
  8. }
  9. #file {
  10. margin: 0;
  11. opacity: 0;
  12. padding: 0;
  13. position: absolute;
  14. top: -10000px;
  15. }
  16. </style>
  17. <paper-icon-button id="btn" icon="chat:attach-file" on-tap="_upload"></paper-icon-button>
  18. <input id="file" type="file" value="{{file::input}}">
  19. </template>
  20. <script>
  21. 'use strict';
  22. Polymer({
  23. is: 'file-sharing-button',
  24. properties: {
  25. file: {
  26. type: String
  27. }
  28. },
  29. attached: function() {
  30. this.$.file.onchange = function(value) {
  31. this.file = this.$.file.value;
  32. console.log(this.file);
  33. var files = this.$.file.files;
  34. for (var i = 0; i < files.length; i++) {
  35. var file = files[i];
  36. var reader = new FileReader();
  37. reader.onload = function(e2) {
  38. // finished reading file data.
  39. console.log('file dropped');
  40. this.fire('file-uploaded', {
  41. url: e2.target.result,
  42. name: file.name
  43. });
  44. }.bind(this);
  45. reader.readAsDataURL(file); // start reading the file data.
  46. }
  47. }.bind(this);
  48. },
  49. _upload: function() {
  50. this.$.file.click();
  51. }
  52. });
  53. </script>
  54. </dom-module>