0

我有一个上传文件功能,我分部分发送文件,提出了很多请求,当我上传我使用的文件async : 'true'$.ajax({});,然后当我点击其他选项卡时,请求获取书名列表,例如......并且请求正在等待并在所有上传请求完成时发出。

我的代码需要一个承诺吗?还是$.when()更好?

我的代码是:

$("#Upload").click(function (){
   ...
   sendFile(file, blockLength, numberOfBlocks).then(()=>{
      //All request are made
   });
});


async function sendFile(file, chunkSize, numberOfBlocks) {
     ...
     function sendNextChunk () {
             $.ajax({
                    async: true,
                    type: 'POST', url: "URL",
                    headers: headers,
                    data: fileChunk,
                    cache: false,
                    contentType: false,
                    processData: false
             }
             ...
             if (currentChunk <= numberOfBlocks) {
                await sendNextChunk();
              }
     }

    await sendNextChunk();

}

在网络中是这样的:

/UPload?chunk=1      200
/UPload?chunk=2      200
/UPload?chunk=3      200
/getBooksList        pending        < - Here is pending until all request of Upload are made
/UPload?chunk=4      200
/UPload?chunk=5      200
/UPload?chunk=6      pending
/UPload?chunk=7      pending
/UPload?chunk=8      pending
/UPload?chunk=9      pending
/UPload?chunk=10     pending
...
4

0 回答 0