d1da59ee9c624d70d8754c5d2733cbef3d681f40
[Plinn.git] / skins / fileupload.js
1 // © 2013 Benoît Pin MINES ParisTech
2 var DDFileUploader;
3
4 (function(){
5
6 DDFileUploader = function(dropbox, uploadUrl) {
7 this.dropbox = dropbox;
8 this.uploadUrl = uploadUrl;
9 var self = this;
10 addListener(dropbox, 'dragenter', function(evt){self.dragenter(evt);});
11 addListener(dropbox, 'dragover', function(evt){self.dragover(evt);});
12 addListener(dropbox, 'drop', function(evt){self.drop(evt);});
13 };
14
15 DDFileUploader.prototype.dragenter = function(evt) {
16 disableDefault(evt);
17 disablePropagation(evt);
18 };
19
20 DDFileUploader.prototype.dragover = function(evt) {
21 disableDefault(evt);
22 disablePropagation(evt);
23 evt = getEventObject(evt);
24 var dt = evt.dataTransfer;
25 dt.dropEffect = 'copy';
26 };
27
28
29 DDFileUploader.prototype.drop = function(evt) {
30 disableDefault(evt);
31 disablePropagation(evt);
32 getEventObject(evt);
33 var dt = evt.dataTransfer;
34 dt.dropEffect = 'copy';
35 this.handleFiles(dt.files);
36 };
37
38 DDFileUploader.prototype.handleFiles = function(files) {
39 var file, i;
40 for (i = 0; i < files.length; i++) {
41 file = files[i];
42 console.log(file.type);
43 this.upload(file);
44 }
45 };
46
47
48 DDFileUploader.prototype.upload = function(file) {
49 var reader = new FileReader();
50 var xhr = new XMLHttpRequest();
51 var percentage;
52 var self = this;
53 // this.xhr.upload.addEventListener("progress", function(e) {
54 // if (e.lengthComputable) {
55 // var percentage = Math.round((e.loaded * 100) / e.total);
56 // self.ctrl.update(percentage);
57 // }
58 // }, false);
59
60 // xhr.upload.addEventListener("load", function(e){
61 // self.ctrl.update(100);
62 // var canvas = self.ctrl.ctx.canvas;
63 // canvas.parentNode.removeChild(canvas);
64 // }, false);
65 console.log(file);
66 xhr.open("PUT", this.uploadUrl + '/' + file.name);
67 xhr.setRequestHeader("Content-Type", file.type);
68 // xhr.overrideMimeType('text/plain; charset=x-user-defined-binary');
69 reader.onload = function(evt) {
70 xhr.sendAsBinary(evt.target.result);
71 };
72 reader.readAsBinaryString(file);
73 };
74
75 }());