JQuery的AJAX的文件下载在IE8,显示黄色的安全栏黄色、文件、安全、JQuery

2023-09-10 19:59:09 作者:空城

我有一个页面,用户可以选择窗体上的一些选项,表单提交事件的帖子这个数据到PHP页面通过阿贾克斯和文件写入到服务器,PHP页面,然后返回文件名和我使用jquery.Download提示用户保存该文件。

I have a page where a user can select some options on a form, the forms submit event posts this data to a PHP page through .AJAX and writes a file to a server, the PHP page then returns the file name and I use jquery.Download to prompt the user to save the file.

这完美的作品在FF​​和铬,但我收到的IE可怕的黄色信息栏。更糟糕的是,如果我的用户从信息栏下载反正,页面只是刷新,而无需实际提示用户下载该文件。

This works perfect in FF and Chrome, but I am receiving the dreaded yellow information bar in IE. To make matters worse, if my user selects "Download anyway" from the information bar, the page just refreshes without actually prompting the user to download the file.

我做的一些#1搜索和它不会出现这个问题已经有了答案。

I've done some searching on Stackoverflow and it doesn't appear this question has been answered.

推荐答案

您所看到的,因为在用户启动浏览器事件和programitically启动浏览器事件定义之间的差异这一点。该烧制作为jQuery的绑定事件的动作的结果通常事件被认为是编程启动即

You are seeing this because of the difference in definition between user initiated browser events and programitically initiated browser event. Generally event that is fired as a result of jQuery binding that event to an action is considered to be programatically initiated i.e.

$("#myElement").click(
    function() {
        $.ajax(...);
    }
);

试图通过一个jQuery 。点击()事件绑定到你的可点击的链接等,以打开新选项卡时,您会看到同样的症状。

You will see this same symptom when trying to open a new tab by binding a jQuery .click() event to your clickable link etc.

一个可能的工作围绕您的问题将是使用调用一个JS函数,你的AJAX调用时即在你的HTML code事件属性

A possible work around to your issue would be to use an event attribute in your HTML code that calls a JS function where your AJAX call is made i.e.

<script type="text/javascript"> 

function submitAndDownload() {
    $.ajax(...); //form submit
    $.ajax(...); //download file
}

</script>

<button onClick="submitAndDownload();"></button>

这样浏览器认为该事件被用户启动。你当然可以嵌套您的表单提交Ajax请求的身体里面你的下载Ajax请求。这将取决于你需要做的。然而,你可能会看到一些不一致的套接字关闭,如果被嵌套人在里面其他的,如果你有在混合其他Ajax调用。

This way the browser considers the event to be user initiated. You could of course nest your download ajax request inside the body of your form submission ajax request. It will depend on what you need to do. However you may see some inconsistency in your socket closure if one is nested inside the other if you have other ajax calls in the mix.