本文介绍了启动文件下载功能很好,即使在IE中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找更多关于从Javascript启动文件下载的最佳方式的想法。

I'm looking for some more thoughts about the best way to initiate a file download from Javascript.

有很多好的想法可以总结:

Best way to initiate a download? has a lot of good ideas which can be summarized:

  • 在iframe上设置src

  • 使用window.location.replace()

  • 使用带有meta刷新标题

  • 使用window.open()

  • 让服务器直接输出文件,设置Content-Type和Content-Disposition

除IE8之外,所有这些方法对于我一直在测试的浏览器都可以正常工作。使用IE8,我遇到一些问题:

All of those approaches work fine for the browsers I've been testing with, except for IE8. With IE8, I get a bunch of problems:

  • 由于环境设置的Cookie,iframe不起作用'使用。我想我需要启用P3P头来解决这个问题,但是环境不允许我设置标题,所以 P3P是出来

  • 窗口.location.replace()的工作除IE8之外,下载的文件打开后,窗口的地址栏将更改为文件的URL,底层窗口为空白

  • 元刷新方法也有效,但文件下载后,地址栏仍然会更改为文件的URL,底层窗口为空白

  • 我拼命地试图避免window.open()到避免任何弹出窗口阻止程序问题

  • 我所在的服务器环境不允许你输出文件本身,就像你可以做的那样,ASP.NET的Response对象

  • The iframe isn't working because of cookies set by the environment I'm using. I think I'd need to enable P3P headers to resolve this, but the environment doesn't allow me to set headers, so P3P is out
  • The window.location.replace() works except that in IE8, the address bar of the window changes to the URL to the file and the underlying window is blank after the downloaded file is opened
  • The meta refresh approach also works, but still the address bar changes to the URL of the file and the underlying window is blank after the file downloads
  • I'm desperately trying to avoid window.open() to dodge any popup blocker problems
  • The server environment I'm in won't allow you to output the file itself, like you could do in say, ASP.NET's Response object

我甚至没有尝试过这些方法IE6或IE7,所以可能会有其他的惊喜。

I haven't even tried these methods with IE6 or IE7, so there may be other surprises there.

所以有人有任何其他建议ini在IE中分层下载,其中(1)不涉及弹出窗口,(2)可以保存或打开文件,(3)不留下空白窗口,(3)仅使用Javascript,HTML和文件的URL

So does anyone have any other suggestions for initiating a download in IE, where (1) no popups are involved and (2) the file can be saved or opened and (3) no blank window is left behind, (3) using just Javascript, HTML and a URL to a file?

丹佛麦克

推荐答案

文件页面(如果你必须有一个;个人我讨厌他们)是:

The usual pattern for a download-file page (if you must have one; personally I hate them) is either:

<script type="text/javascript">
    window.onload= function() {
        window.location= document.getElementById('downloadlink').href;
    }
</script>
<p>
    Your download will begin shortly. If it doesn't,
    <a id="downloadlink" href="file.zip">click here</a>.
</p>

或者与元刷新而不是脚本相同。任何一种方式都应该表现得非常相似。

Or the same with a meta-refresh instead of the script. Either way should behave pretty much the same.

这不应该发生。是否有在线版本可以测试?通常情况下,当浏览器通过任何常规方法(链接click,location.href,元刷新)推送到直接文件下载时,应该保持屏幕上的前一页。

That shouldn't happen. Is there a version online that can be tested? Normally, when the browser is pushed to a direct file download by any normal method (link click, location.href, meta-refresh), it should keep the previous page on-screen.

你不必使用标题来设置P3P策略,HTML< link>标签也是一样:

You don't have to use headers to set P3P policies, an HTML <link> tag works just as well:

<link rel="P3Pv1" href="/policy.p3p" />

但是你为什么需要?如果目标网址只是提供一个文件,那么没有必要设置一个cookie,所以为什么要打扰P3P?

But why would you need to? If the target URL is just serving a file, there is no need to set a cookie at all, so why bother with P3P?

如果你的window.open()响应一个用户点击,没有弹出窗口阻止程序的问题。

If you window.open() in response to a user click, there are no pop-up blocker problems.

不是你应该需要打开一个弹出窗口只是为了下载文件。我开始认为您链接到的文件下载目的地有一些非常奇怪的东西 - 就像它不是一个文件下载,但是一些奇怪的HTML网页应用程序。链接到下载不难,你只需链接到文件,完成工作;你似乎比这本身更加困难。

Not that you should need to open a pop-up just to download a file anyway. I am beginning to think there is something highly strange about the file download destination you are linking to — like it's not a file download at all but some kind of odd HTML web app. Linking to a download is not hard, you just link to the file, job done; you seem to be making this much harder than it intrinsically is.

只是链接到一个文件的唯一常见的问题是,如果它包含文本,HTML,XML或图像,浏览器将内嵌显示,而不是下载它。打败这个的唯一方法是使用Content-Disposition:attachment标题,通过设置此标题的脚本来提供它,或者通过配置您的Web服务器来发送所有文件下载的标题。如果您无法在服务器环境中执行任何操作,则无法解决问题。

The only usual problem with just linking to a file is that if it contains text, HTML, XML or an image, the browser will display it inline instead of downloading it. The only way to defeat this is using the ‘Content-Disposition: attachment’ header, either by serving it through a script that sets this header, or by configuring your web server to send that header for all file downloads. If you can't do either of those in your server environment, there is no solution.

这篇关于启动文件下载功能很好,即使在IE中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 19:08