1var global = this; 2global.uploadlib = new function() { 3 4 var uploadlib = this; 5 6 var this_upload_handler = null; 7 var this_upload_files = null; 8 var this_upload_index = 0; 9 var this_upload_total = 0; 10 var f_offset = 0; 11 //var segment_size = 50*1024; 12 var segment_size = 5*1024*1024; //- 5MB 13 14 //var loop_cnt = 0; 15 //var current_file_size; 16 var this_file_loaded_size = 0; 17 var this_file_total_loaded_size = 0; 18 var this_upload_total_size = 0; 19 20 var this_progress_callbackfunction = null; 21 var this_complete_callbackfunction = null; 22 23 this.WebDAVUploadHandler = function() { 24 this_upload_handler = this; 25 }; 26 27 this.WebDAVUploadHandler.prototype.initialize = function(_url, _files, _progress_callbackfunction, _complete_callbackfunction){ 28 this_upload_files = _files; 29 this_progress_callbackfunction = _progress_callbackfunction; 30 this_complete_callbackfunction = _complete_callbackfunction; 31 32 this_upload_handler.url = _url; 33 34 this_upload_handler.webdav = new davlib.DavClient(); 35 this_upload_handler.webdav.initialize(); 36 37 this_upload_index = 0; 38 this_upload_total = 0; 39 f_offset = 0; 40 //loop_cnt = 0; 41 //current_file_size = 0; 42 this_file_loaded_size = 0; 43 this_file_total_loaded_size = 0; 44 this_upload_total_size = 0; 45 this_upload_total = this_upload_files.length; 46 f_offset =0; 47 48 for(var i = 0; i < this_upload_files.length; i++){ 49 this_upload_total_size += this_upload_files[i].thefile.size; 50 } 51 }; 52 53 this.WebDAVUploadHandler.prototype.uploadFile = function() { 54 for (var i = 0, f; f = this_upload_files[i]; i++) { 55 //loop_cnt = Math.ceil(f.thefile.size/segment_size); 56 //current_file_size = f.thefile.size; 57 58 if(f.status=="Init" || f.status=="Upload"){ 59 var filesize = f.thefile.size; 60 this_upload_index = i; 61 f.status = "Upload"; 62 63 var start = f_offset; 64 var stop = start+ segment_size-1; 65 if(stop>= filesize){ 66 stop = filesize - 1; 67 //alert("stop="+stop+", filesize="+filesize); 68 f.status = "end_send"; 69 } 70 f_offset = stop+1; 71 //alert("start=" + start + ", stop=" + stop+ ", filesize=" + filesize); 72 this_upload_handler._putToWeb(f.thepath, f.thefile, start, stop, filesize); 73 74 return 1; 75 } 76 } 77 return 0; 78 }; 79 80 this.WebDAVUploadHandler.prototype._putToWeb = function(path, pfile, opt_startByte, opt_stopByte, filesize){ 81 var start = opt_startByte; 82 var stop = opt_stopByte; 83 84 try{ 85 var reader = new FileReader(); 86 var bSliceAsBinary = 0; 87 88 // If we use onloadend, we need to check the readyState. 89 reader.onloadend = function(evt) { 90 if (evt.target.readyState == FileReader.DONE) { // DONE == 2 91 //alert(evt.target.result.byteLength); 92 93 var tURL = this_upload_handler.url + encodeURIComponent(path) + encodeURIComponent(pfile.name); 94 95 this_upload_handler.webdav.PUT(tURL, 96 evt.target.result, 97 bSliceAsBinary, 98 start, 99 stop, 100 filesize, 101 "T", 102 this_upload_handler._complete_callbackfunction, 103 this_upload_handler._progress_callbackfunction); 104 } 105 }; 106 107 if (pfile.webkitSlice) { 108 var blob = pfile.webkitSlice(start, stop + 1); 109 reader.readAsArrayBuffer(blob); 110 bSliceAsBinary = 1; 111 } 112 else if (pfile.mozSlice) { 113 var blob = pfile.mozSlice(start, stop + 1); 114 reader.readAsBinaryString(blob); 115 bSliceAsBinary = 0; 116 117 //reader.readAsArrayBuffer(blob); 118 //bSliceAsBinary = 1; 119 } 120 else { 121 var blob = pfile.slice(start, stop + 1); 122 reader.readAsArrayBuffer(blob); 123 bSliceAsBinary = 1; 124 } 125 } 126 catch(err){ 127 alert(err.message); 128 } 129 }; 130 131 this.WebDAVUploadHandler.prototype._progress_callbackfunction = function(evt){ 132 if(this_upload_files.length<=0) 133 return; 134 135 if(evt.lengthComputable) { 136 //this_file_loaded_size = this_file_loaded_size+evt.loaded; 137 var f = this_upload_files[this_upload_index]; 138 139 if(f==null || f==undefined){ 140 //alert(this_upload_index); 141 return; 142 } 143 144 var isUploadSegmentOK=0; 145 if(evt.loaded>0 && (evt.loaded/evt.total==1)) { 146 isUploadSegmentOK=1; 147 } 148 149 if(isUploadSegmentOK==1) { 150 //var size = evt.loaded; 151 var size = segment_size; 152 153 this_file_loaded_size = this_file_loaded_size + size; 154 this_file_total_loaded_size = this_file_total_loaded_size + size; 155 //alert("segment =1 , loadded size ="+evt.loaded+", evt.total="+evt.total+", size="+size); 156 } 157 158 var file_upload_percent = Math.min(100, 100*(this_file_loaded_size/f.thefile.size)); 159 var total_upload_percent = Math.min(100, 100*(this_file_total_loaded_size/this_upload_total_size)); 160 161 if(this_progress_callbackfunction){ 162 this_progress_callbackfunction(f.id, f.thefile.name, f.status, file_upload_percent, total_upload_percent); 163 } 164 } 165 }; 166 167 this.WebDAVUploadHandler.prototype._complete_callbackfunction = function(error, content){ 168 if(this_upload_files.length<=0) 169 return; 170 171 var f = this_upload_files[this_upload_index]; 172 var all_complete = false; 173 174 if(f==null || f==undefined){ 175 //alert(this_upload_index); 176 return; 177 } 178 179 if(error){ 180 if(error==200||error==201||error==204 ) { 181 if(this_upload_files[this_upload_index].status=="end_send"){ 182 //alert("file transferred done....call this upload files splice"); 183 //this_upload_files.splice(this_upload_index,1); 184 this_upload_files[this_upload_index].status = "done"; 185 f_offset = 0; 186 this_file_loaded_size = 0; 187 } 188 } 189 else if(error==0){ 190 this_upload_files[this_upload_index].status = "UploadFail"; 191 } 192 else{ 193 this_upload_files[this_upload_index].status = "UploadFail"; 194 } 195 } 196 197 if( this_upload_handler.uploadFile() == 0 ){ 198 all_complete = true; 199 } 200 201 if(this_complete_callbackfunction) 202 this_complete_callbackfunction(f.id, f.thefile.name, f.status, error, all_complete); 203 }; 204 205}();