/* Upload a file >2GB in javascript */ function Uploader(f) { this.FileName=f.name; this.Size=f.size; this.f=f; this.PacketSize=1024*1024; // 1MB this.TotalPackets=Math.ceil(this.Size/this.PacketSize); this.lastPacket=0; this.guid=""; this.state=0; this.progress=null; this.cancelled=null; this.complete=null; this.fileno=0; this.nfiles=1; this.NewFile = function(f,idx) { this.FileName=f.name; this.Size=f.size; this.f=f; this.PacketSize=1024*1024; // 1MB this.TotalPackets=Math.ceil(this.Size/this.PacketSize); this.lastPacket=0; this.state=0; this.fileno=idx; }; this.Start = function(idx,n) { this.state=1; var obj=new Object(); obj.filename=this.FileName; obj.filesize=this.Size; obj.totalpackets=this.TotalPackets; obj.packetsize=this.PacketSize; obj.fileno=idx; obj.nfiles=n; if ((this.guid) && (this.guid.length>0)) obj.Guid=this.guid; var formData=JSON.stringify(obj); this.fileno=idx; this.nfiles=n; var obj = this; var xhr = new window.XMLHttpRequest(); xhr.open('POST', API_URL("upload/startupload",0,true), true); xhr.setRequestHeader("Content-Type", "application/json"); xhr.onload = function (e) { obj.onInitPacketArrival(this); }; xhr.send(formData); }; this.onInitPacketArrival = function(xmlHttpReq) { var resp=JSON.parse(xmlHttpReq.responseText); this.guid=resp.Guid; this.uploadPacket(); }; this.uploadPacket = function() { var packet=this.lastPacket+1; if (packet>this.TotalPackets) return; if (this.state==0) return; var xhr = new window.XMLHttpRequest(); var url = API_URL("upload/uploadpacket?guid=" + this.guid + "&packet=" + packet + "&total=" + this.TotalPackets,0,true); var obj = this; xhr.open('POST', url, true); xhr.onprogress = function (e) { }; xhr.onload = function (e) { obj.onPacketArrival(this); }; xhr.send(this.getDataPacket(packet)); if (this.progress) { var pct=Math.ceil(100*packet/this.TotalPackets); this.progress(pct); } }; this.getDataPacket = function (packetNumber) { var startByte = (packetNumber - 1) * this.PacketSize; var endByte = startByte + this.PacketSize; var packet; // the HTML5 dirty part, FileReader API is not yet fully implemented //packet = this.file.slice(startByte, endByte); if ('mozSlice' in this.f) { // mozilla packet = this.f.mozSlice(startByte, endByte); } else if ('webkitSlice' in this.f) { // webkit packet = this.f.webkitSlice(startByte, endByte); } else { // IE 10 packet = this.f.slice(startByte, endByte); } return packet; }; this.onPacketArrival = function(xmlHttpReq) { if (this.state==0) return; var response = JSON.parse(xmlHttpReq.responseText); // if packet == -1 that means no retry possible if (response.Packet == -1) { //this.guid = ""; this.lastPacket = 0; this.Start(this.fileno,this.nfiles); return; } if (response.Packet==-2) { // cancel this.guid=""; this.lastPacket=0; if (this.cancelled) this.cancelled(); return; } this.guid = response.Guid; this.lastPacket = response.Packet; // if last packet was received OK if (this.lastPacket == this.TotalPackets) { this.state = 0; // stoped if (this.complete) this.complete(); } else this.uploadPacket(); }; this.cancel = function() { var xhr = new window.XMLHttpRequest(); xhr.open('GET', API_URL("upload/cancel/"+this.guid,0,true), true); xhr.setRequestHeader("Content-Type", "application/json"); xhr.send(); }; }