如何确保文件是否已成功下载

问题描述 投票:0回答:1

在我的项目中,我想下载文件,下载后将删除此文件。

我在第一个ajax中设置了async:false以确认下载文件已完成。

 //this is download file code:
 $.ajax({
        dataType:'json',
        type:'POST',
        async: false,
        data:{files:files},
        url:'oat.php',
        success:function(data){
            delFile=data.name;
            alert(delFile);
            window.location=delFile;
        }
    });

    //this is delete file code:
    $.ajax({
           type:'POST',
           data:{delFile:delFile},
           url:'delFileoat.php',
           success:function(data){
            }
        });

但不幸的是,删除总是在下载之前,因为它需要时间下载,但删除文件只是一秒钟。

所以我总是得到关于“无法在服务器中找到文件”的消息。

成功下载文件后是否有window.location=delFile的返回值?

javascript php ajax
1个回答
1
投票

async: false已被弃用。您可以在success回调中删除您的文件。如下

$.ajax({
  dataType: 'json',
  type: 'POST',
  data: {
    files: files
  },
  url: 'oat.php',
  success: function(data) {
    // do your stuff
    $.ajax({
      type: 'POST',
      data: {
        delFile: delFile
      },
      url: 'delFileoat.php',
      success: function(data) {}
    });
  }
});
© www.soinside.com 2019 - 2024. All rights reserved.