简体   繁体   English

Javascript从服务器下载zip文件

[英]Javascript download zip file from server

I have a zip file in a predefined location on my web application server. 我的Web应用程序服务器上的预定义位置有一个zip文件。 I'm trying to download the file using Javascript and i can get the following to work in FF but not in Chrome. 我正在尝试使用Javascript下载文件,并且我可以让以下文件在FF中工作,但不能在Chrome中工作。 Has anyone come across a better way to do this that is cross browser friendly way (FF/Chrome/IE)? 有没有人遇到跨浏览器友好的方法(FF / Chrome / IE)更好的方法呢? If i could force the Save as prompt even better. 如果我可以强制另存为提示甚至更好。 Im using the dojo toolkit if it helps 我正在使用dojo工具包,如果有帮助

function downloadZip() {

    var a = document.createElement("a");

    document.body.appendChild(a);
    a.href     = "/path_to_file/my.zip";

    a.click();
}

Try this. 尝试这个。

function downloadURI(uri, name) 
{
    var link = document.createElement("a");
    link.download = name;
    link.href = uri;
    link.click();
}

I have used dojo/request/iframe for this. 我为此使用了dojo/request/iframe This is my exact code: 这是我的确切代码:

iframe._currentDfd = null; // Force Dfd to be empty in case prior calls have not been cleared
iframe.post('path_to_file/file.zip',{
            data: params,
            handleAs: "html"
            }).then(function (data) {
                 console.log('Then called' + data);
            }, function (err) {
                 console.log(err);
});

This works in Chrome, Firefox and IE. 这适用于Chrome,Firefox和IE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM