JavaScript:如何通过 AJAX 打开返回的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11006055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:47:26  来源:igfitidea点击:

JavaScript: How to open a returned file via AJAX

javascriptjquerydownload

提问by vol7ron

This is similar to: How to open a file using JavaScript?

这类似于:如何使用 JavaScript 打开文件?

Goal: to retrieve/open a file on an image's double click

目标:通过双击图像检索/打开文件

function getFile(filename){
   // setting mime this way is for example only
   var mime = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';

   jQuery.ajax({ url      : 'get_file.pl',
                 data     : {filename:filename}, 
                 success  : function(data){
                               var win = window.open('','title');
                               win.document.open(mime);
                               win.document.write(data);
                               win.document.close();
                            }
               });
}

jQuery('#imgID').dblclick(function(){ 
   getFile('someFile.docx');
});

I'm doing this off the top of my head, but I think the above would work for text files, but not binary. Is there a plugin that does this properly? The ideal would be to open the file in the browser (or application), rather than download, but I doubt that is a dream. If the file must be downloaded with the save/open dialog, that's fine.

我正在做这件事,但我认为上述内容适用于文本文件,但不适用于二进制文件。有没有插件可以正确执行此操作?理想的情况是在浏览器(或应用程序)中打开文件,而不是下载,但我怀疑这是一个梦想。如果必须使用保存/打开对话框下载文件,那很好。



Edit:

编辑:

One piece of information that I forgot to mention is that I'd like this to be a POST request. This is partly why I was looking at AJAX to begin with. I've seen workarounds that have created forms/iframes to do something similar, but I was looking for a better handler of the returned info.

我忘记提及的一条信息是我希望这是一个 POST 请求。这也是我开始研究 AJAX 的部分原因。我已经看到创建表单/iframe 来做类似事情的解决方法,但我正在寻找一个更好的返回信息处理程序。

回答by josh3736

Seems to me there's no reason to do this via AJAX. Just open the new window to get_file.pl?filename=...and let the browser handle it. If the user has a plugin capable of handling the Content-Typesent by get_file.pl, the file will display; otherwise, it should download like any other file.

在我看来,没有理由通过 AJAX 执行此操作。只需打开新窗口get_file.pl?filename=...并让浏览器处理它。如果用户有一个能够处理get_file.plContent-Type发送的插件,该文件将显示;否则,它应该像任何其他文件一样下载。

function getFile(filename) {
   window.open('get_file.pl?filename=' + filename,'title');
}

jQuery('#imgID').dblclick(function() { 
   getFile('someFile.docx');
});


Edit: If you want to POSTto your script, you can do itwith some <form>hackery:

编辑:如果你想要POST你的脚本,你可以用一些<form>hackery来做

function getFile(filename) {
    var win = 'w' + Math.floor(Math.random() * 10000000000000);
    window.open('', win,'width=250,height=100');
    var f = $('<form></form>')
            .attr({target: win, method:'post', action: 'get_file.pl'})
            .appendTo(document.body);

    var i = $('<input>')
            .attr({type:'hidden',name:'filename',value:filename})
            .appendTo(f);

    f[0].submit();
    f.remove();
}

Of course, this is somewhat silly since it is impossible to hide your data from "prying eyes" with developer tools. If your filename really is sensitive, issue access tokens to the client, and look up the data in your sever script.

当然,这有点愚蠢,因为使用开发人员工具不可能隐藏您的数据不被“窥探”。如果您的文件名确实很敏感,请向客户端发出访问令牌,并在您的服务器脚本中查找数据。