html5 xhr,Using xhr object for html5 chunked upload in a loop

问题

Experts, I am facing issues looping through the BLOB chunks I create for resumable chunked upload. The method I am following is as:

I chunk the file to be uploaded using Blob.slice(), push all the chunks on a queue and try to send them to my server.

The issue I am facing is regarding looping through the chunks. The first chunk is sent successfully and I end up in uploadNext method mentioned below from the function delegated to xhr.onreadystatechange. All good till now, but this piece of code just stops after sending the first two chunks and doesn't loop through.

The following code is what I have achieved so far:

uploadFile: function(item, location, start, isresume) {

var blob = item.file;

const BYTES_PER_CHUNK = 1024 * 200 * 10;

const SIZE = blob.size;

var chunkId = 0;

var end = BYTES_PER_CHUNK;

while(start < SIZE) {

var chunkObj = {};

// Note: blob.slice has changed semantics and been prefixed. See http://goo.gl/U9mE5.

end = start + BYTES_PER_CHUNK;

if(end > SIZE){

end = SIZE;

}

if ('mozSlice' in blob) {

chunkObj["chunk"] = blob.mozSlice(start, end);

chunkObj["id"] = chunkId;

} else {

// var chunk = blob.webkitSlice(start, end);

chunkObj["chunk"] = blob.slice(start, end);

chunkObj["start"] = start;

chunkObj["end"] = end-1;

chunkObj["id"] = chunkId;

}

chunkId++;

item.chunks.push(chunkObj);

start = end ;

}

this.upload(item, location, SIZE, isresume);

},

upload: function(item, location, SIZE, isresume) {

var xhr = new XMLHttpRequest();

// Upload the first chunk

xhr.upload.addEventListener("error", this.onFileUploadError.createDelegate(this, [item, xhr], 0), false);

xhr.onload = this.getStatus.createDelegate(this, [item, xhr, isresume], 0);

xhr.onreadystatechange = this.isChunkUploaded.createDelegate(this, [item, item.chunks[0], xhr, 0, SIZE, location, isresume], 0);

xhr.open("PUT", location, true);

xhr.setRequestHeader("Content-Range", "bytes " + item.chunks[0].start + "-" + item.chunks[0].end + "/" + SIZE);

xhr.send(item.chunks[0].chunk);

},

uploadNext: function(item, xhr, index, SIZE, location, isresume) {

if(index > item.chunks.length) {

return;

}

console.log("uploading next chunk");

xhr = new XMLHttpRequest();

xhr.upload.addEventListener("error", this.onFileUploadError.createDelegate(this, [item, xhr], 0), false);

xhr.onload = this.getStatus.createDelegate(this, [item, xhr, isresume], 0);

xhr.onreadystatechange = this.isChunkUploaded.createDelegate(this, [item, xhr], 0);

xhr.open("PUT", location, true);

xhr.setRequestHeader("Content-Range", "bytes " + item.chunks[index].start + "-" + item.chunks[index].end + "/" + SIZE);

xhr.send(item.chunks[index].chunk);

//this.uploadNext(item, xhr, index + 1, SIZE, location, isresume);

},

isChunkUploaded: function(item, chunkObj, request, index, SIZE, location, isresume, e) {

if (request.readyState === 4) {

console.log("chunk " + index + " uploaded");

this.uploadNext(item, request, index + 1, SIZE, location, isresume);

}

},

P.S. I can't use jquery for the same (don't ask why).

回答1:

Nevermind, this is working correctly. I made an error on my end by sending less function parameters as function arguments than expected.

来源:https://stackoverflow.com/questions/12235709/using-xhr-object-for-html5-chunked-upload-in-a-loop

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值