bg.js
var blob = new Blob(['dummy content'], {type: 'text/plain'});
var url = URL.createObjectURL(blob);
var fileName = fileNamePrefix + (new Date()).getTime();
console.log('debug: fileNamePrefix: ' + fileNamePrefix);
console.log('debug: fileName: ' + fileName + '.txt');
chrome.downloads.download({
url: url,
filename: fileName + '.txt',
saveAs: false
}, function(id) {
console.log('debug: download callback: id: ' + id);
});
控制台输出
bg.js:11 debug: fileNamePrefix: dummy-prefix--
bg.js:12 debug: fileName: dummy-prefix--1514054609088.txt
bg.js:18 debug: download callback: id: 39
的manifest.json
{
"name": "DemoExt",
"description": "",
"version": "0.1",
"minimum_chrome_version": "16.0.884",
"permissions": ["contextMenus", "downloads", "storage", "<all_urls>"],
"background": {"scripts": ["bg.js"], "persistent": false},
"manifest_version": 2
}
为什么文件名是所有ID,而不是我提供的文件名?
Robs-Mac:~ rob$ ls Downloads/
31f4efac-2fc9-40e5-bb0b-c5aaa95d1212.txt
49833a51-e3a1-4c5a-a2c6-1f209eae7a34.txt
4b045f60-7359-42fe-89ae-df09f42a6777.txt
6f138ef2-f9da-4783-98d2-012a0fea533d.txt
6fb5f779-385e-4516-989c-aed2691e21b7.txt
答案 0 :(得分:-2)
我没有使用chrome扩展,但我以不同的方式做同样希望这对你有所帮助。这是我使用的代码
var blob = new Blob(['dummy content'], {type: 'text/plain'});
var fileName = "dummyprefix.txt";
var pom = document.createElement('a');
document.body.appendChild(pom);
pom.style = "display: none";
var url = window.URL.createObjectURL(blob)
pom.setAttribute('href', url);
pom.setAttribute('download', fileName);
pom.dataset.downloadurl = ['text/plain', pom.download, pom.href].join(':');
pom.click();
window.URL.revokeObjectURL(url);
您可能需要添加对Internet Explorer的检查。