如何使用 iframe.src 下载文件时获取返回状态代码

how can I get the return status code when using iframe.src to download a file?

本文关键字:获取 返回 状态 代码 文件 何使用 iframe src 下载      更新时间:2023-09-26

这是我用来下载文件的downloadURL函数:

function downloadURL(url) {
    var hiddenIFrameID = 'hiddenDownloader',
    iframe = document.getElementById(hiddenIFrameID);
    if (iframe === null) {
    iframe = document.createElement('iframe');
    iframe.id = hiddenIFrameID;
    iframe.style.display = 'none';
    document.body.appendChild(iframe);
    }
    iframe.src = url;
};

这可以完美地用于下载文件,但是现在,我想获取返回的http状态代码,如果http状态代码不是200,我想alert一些东西。

谁能告诉我如何检索它?提前谢谢。

似乎不可能。您可以尝试使用 ajax,如下所示:使用 javascript 检查 http 状态代码