嗨,我正在下载使用Chrome扩展选定的链接,但我无法设置下载位置。所有的网址都下载到Chrome的默认位置。我知道我们不能这样做是因为安全原因。我们可以在这里提示在Chrome扩展弹出窗口中的目录选择器对话框,用户可以选择下载路径。需要我的任何信息让我知道。如何设置Chrome扩展的文件下载位置使用JavaScript?
这可能吗?有关如何解决它的任何建议?
在此先感谢 我的代码
function downloadFile(url, onSuccess,arrayOfUrl,zip) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = "blob";
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (onSuccess)
{
onDownloadComplete(xhr.response, arrayOfUrl,zip)
}
}
}
xhr.send("null");
}
function onDownloadComplete(blobData,urls,zip){
if (count < urls.length) {
blobToBase64(blobData, function(binaryData){
var fileName = urls[count].substring(urls[count].lastIndexOf('/')+1);
zip.file(fileName+".docx", binaryData, {base64: true});
if (count < urls.length -1){
count++;
downloadFile(urls[count], onDownloadComplete, urls,zip);
}
else {
var content = zip.generate();
var zipName = 'download.zip';
var a = document.createElement('a');
a.href = "data:application/zip;base64," + content;
a.download = zipName;
a.click();
count = 0;
}
});
}
}
popup.js
function onDownloadComplete(blobData,urls,zip){
if (count < urls.length) {
blobToBase64(blobData, function(binaryData){
// add downloaded file to zip:
var fileName = urls[count].substring(urls[count].lastIndexOf('/')+1);
// zip.file(fileName, binaryData, {base64: true});
zip.file(fileName+".docx", binaryData, {base64: true}); //file"+count+".docx"
if (count < urls.length -1){
count++;
downloadFile(urls[count], onDownloadComplete, urls,zip);
}
else {
chrome.runtime.getBackgroundPage(function() {
zipAndSaveFiles(zip);});
}
});
}
}
**background.js**
function zipAndSaveFiles(zip)
{
var content = zip.generate(zip);
var zipName = 'download.zip';
var dataURL = 'data:application/zip;base64,' + content;
chrome.downloads.download({
url: dataURL,
filename: zipName,
saveAs: true
});
}
你看过** ['chrome.d ownloads.download()'](http://developer.chrome.com/extensions/downloads.html#method-download)**? – gkalpak
是的,我知道,但我想提供一些关于此的更多信息。我正在使用JsZip库构建一个包含所有可下载文件的zip文件。我需要在扩展弹出窗口中提示目录选择器路径,以便保存zip文件的位置。它可能吗?提供任何链接和示例代码来完成this.and我的代码看到我更新的问题 –
@ExpertSystem是我更新的代码给你任何想法?让我知道你是否需要进一步的信息。使用FileSystem API可以做到这一点因为这是紧急的 –