JavaScript:如何通过AJAX打开返回的文件 [英] JavaScript: How to open a returned file via AJAX

查看:141
本文介绍了JavaScript:如何通过AJAX打开返回的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

推荐答案

对我来说,没有理由通过AJAX这样做。只需打开 get_file.pl?filename = ... 的新窗口,让浏览器处理它。如果用户有一个能够处理 get_file.pl 发送的 Content-Type 的插件,则会显示该文件;否则,它应该像任何其他文件一样下载。

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-Type sent by get_file.pl, the file will display; otherwise, it should download like any other file.

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

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






编辑:如果你希望 POST 到你的脚本,你可以这样做有一些< form> hackery:


Edit: If you want to POST to your script, you can do it with some <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.

这篇关于JavaScript:如何通过AJAX打开返回的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆