jquery 文件有时在下载完成前被删除

yc0p9oo0  于 4个月前  发布在  jQuery
关注(0)|答案(1)|浏览(65)

我有一个页面,用户点击一个按钮来下载文件。它应该解密文件,下载文件,然后删除解密文件。间歇发生的是,删除是在下载完成之前发生的。有时文件被部分下载,有时根本没有。
我通常会在服务器端下载文件,但由于这是一个AJAX页面,我使用的方法似乎不起作用。所以我正在从服务器端和客户端来回做一些事情。
下面是VB.NET服务器端的子按钮点击后触发:

Sub DownloadFile()
    output = MapPath + _hdnFilePath.Value
    input = output.Replace("_dec", "_enc")
    crptFile.Decrypt(input, output)
    Dim encodedFileName As String = _hdnFilePath.Value.Replace("'", "@@@")
    RadAjaxManager1.ResponseScripts.Add("downloadURI('" & encodedFileName & "');")
    ''OLD  DOWNLOAD METHOD BELOW - does not work with ajax
    'Response.Clear()
    'Response.ContentType = "application/pdf"
    'Response.AppendHeader("Content-Disposition", "attachment; filename=" + 
    'Path.GetFileName(output))
    'Response.WriteFile(output)
    'Response.Flush()

    'Delete the decrypted (output) file.
    'File.Delete(output)
End Sub

字符串
下面是这个函数触发的JavaScript:

function downloadURI(encodedFileName) {
    encodedFileName = encodedFileName.replace(/\@\@\@/g, "'");
    var encodedURI = encodeURIComponent(encodedFileName)
    encodedFileName = encodedFileName.replace("###", "%").replace("$$$", "&")
    fetch('<%= ResolveUrl("/CUTracking/CUFiles/") %>' + encodedURI)
        .then(resp => resp.blob())
        .then(blob => {
            const url = window.URL.createObjectURL(blob);
            const a = document.createElement('a');
            a.style.display = 'none';
            a.href = url;
            a.download = encodedFileName;
            document.body.appendChild(a);
            a.click();
            window.URL.revokeObjectURL(url);
            a.remove();
        })
        .catch(() => alert('There was an error'));
    $find("<%= RadAjaxManager1.ClientID %>").ajaxRequest("Delete")
}


下面是由JavaScript下载函数触发的删除VB.NET子程序:

Protected Sub RadAjaxManager1_AjaxRequest(sender As Object, e As 
    AjaxRequestEventArgs)
    If e.Argument = "Delete" Then
        File.Delete(MapPath + _hdnFilePath.Value)
    End If
End Sub


有没有什么方法可以防止文件被过早删除,或者有没有一个不那么迂回的方法来解决这个问题?我只使用一个blob来解决这个问题,只使用JavaScript下载文件,因为我的服务器端方法不支持AJAX。

lc8prwob

lc8prwob1#

这个建议不能解决你面临的同步问题,但是你的下载脚本还有改进的空间。
当您创建一个临时元素(在本例中是一个锚)时,不需要将其附加到文档中.

const url = window.URL.createObjectURL(blob);

const a = document.createElement('a');

a.style.display = 'none';   // <-- You don't need this

a.href = url;

a.download = encodedFileName;

document.body.appendChild(a);  // <-- You don't need this

a.click();

window.URL.revokeObjectURL(url);

a.remove();  // <-- You don't need this

字符串
此外,下载应该在一个新的窗口中打开,从而导致这个改进的版本.

var url = window.URL.createObjectURL(blob);

var a = document.createElement('a');

a.target = '_blank'; // <-- new window

a.href = url;

a.download = encodedFileName;

a.click();

window.URL.revokeObjectURL(url);


我改变的另一件事是将const声明改为vars。
常数应该用于永远不会改变的数据,例如一周中的几天或一年中的几个月,如下所示:

const DaysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];


请注意,您正在明智地撤销(删除/编辑)blob,因此它应该是一个var。
常量声明告诉O/S在程序运行期间为该声明保留确切的内存大小,因为数据永远不会改变,但你希望那个blob消失,占用的内存被释放,对吗?

相关问题