在 popup.js firefox 插件中使用 browser.downloads.download() API 下载失败

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

我在 firefox 扩展的 popup.js 中有这个函数,它应该下载代码中生成的文件。 下载已初始化,但随后失败,除了浏览器下载部分中显示“下载失败”的文件外,没有任何明显错误。 我已经用互联网上的网址尝试过,效果很好。

function download() {
  const blob = new Blob(["smssjsjssjsj"], { type: "text/plain" });

  // Create a URL for the Blob
  const url = URL.createObjectURL(blob);
  // Initiate the file download using the downloads API

  browser.downloads.download(
    {
      url: "url",
      filename: `xxxa.txt`,
      saveAs: false,
    },
    (downloadId) => {
      if (chrome.runtime.lastError) {
        errmessage.innerText =
          "Download failed:" + chrome.runtime.lastError.message;
      }
    }
  );
  URL.revokeObjectURL(url);
}
javascript firefox-addon
1个回答
0
投票
function download() {
  const blob = new Blob(["smssjsjssjsj"], { type: "text/plain" });
  const url = URL.createObjectURL(blob);
  browser.downloads.download({
    url: url,
    filename: `xxxa.txt`,
    saveAs: false
  }, (downloadId) => {
    if (downloadId) {
      console.log(`Download initiated with ID: ${downloadId}`);
      setTimeout(() => {
        URL.revokeObjectURL(url);
      }, 10000); // 10-second delay before revoking URL, adjust as necessary
    } else if (chrome.runtime.lastError) {
      console.error("Download failed: " + chrome.runtime.lastError.message);
    }
  });
}
© www.soinside.com 2019 - 2024. All rights reserved.