首页 > 解决方案 > 下载带有标题的文件请求 chrome 扩展

问题描述

我正在开发 google chrome 扩展程序,我想在请求中发送标头,例如:

chrome.downloads.download({
    url: 'http://test/api/file/download',
    filename: "file_from_web_api.exe",
    headers: {
        ProfileID: "1"
    }
});

但我收到错误:

未捕获的类型错误:调用 downloads.download 时出错(downloads.DownloadOptions 选项,可选函数回调):参数“选项”错误:属性“标题”错误:无效类型:预期数组,找到对象。

我的问题是如何将headres附加到下载请求

标签: google-chrome-extensiondownload

解决方案


根据文档标题需要是一个对象数组

chrome.downloads.download({
    url: 'http://test/api/file/download',
    filename: "file_from_web_api.exe",
    headers: [
        {'ProfileID': '1'}
    ]
});

您也可以尝试先创建一个标头对象,然后将其添加到数组中看看这里

编辑:尝试使用标头对象

chrome.downloads.download({
    url: 'http://test/api/file/download',
    filename: "file_from_web_api.exe",
    headers: new Headers({
        'ProfileID': '1'
    })
});

推荐阅读