如何在不离开页面的情况下提供文件? [英] How do you serve a file without leaving the page?

查看:49
本文介绍了如何在不离开页面的情况下提供文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过单击页面上的按钮让用户下载文件(在这种情况下为myfile.zip),而没有他们离开页面-即浏览器必须保持当前状态页面,然后将其放置在可以继续使用该页面的位置,包括再次单击该按钮(他们希望获取文件的新副本).

I'm trying to let users download a file (myfile.zip in this case) by clicking a button on the page, without them leaving the page - ie the browser must stay on the current page, and leave them in a position where they can continue to use the page, including clicking the button again (should they wish to get a new copy of the file).

我需要它才能在所有浏览器(IE6-8,Firefox,Chrome,Opera,Safari)中正常工作.

I need this to work across all browsers (IE6-8, Firefox, Chrome, Opera, Safari).

在zip内包装的是基于同一页面中其他交互(某些交互可能已部分完成)的选择内容(这全部是通过ajax完成的),我不希望他们将页面保留为他们将丢失所有未保存的更改.

Packaged inside the zip is a selection of stuff based on their other interactions (some of which may be partially complete) from the same page (this is all done via ajax) and I don't want them to leave the page as they would lose any unsaved changes.

推荐答案

为避免离开页面(如果执行此操作,页面将首先尝试自行关闭,以确保您已保存所有内容并得到警告消息(如果没有)或留下空白标签(我不喜欢,也不使用已贬值的target属性),我使用的是iframe,其src属性在javascript中已更改.

To avoid leaving the page (if you do this the page tries to close itself first, so that it's sure that you've saved everything, and you get warning messages if you haven't) or leaving blank tabs (which I don't like, nor the use of the depreciated target attribute) I've used an iframe, whose src attribute is changed in javascript.

除了某些版本的Opera(我认为这是可以接受的损失)之外,此方法在任何地方都可以使用(我可能会通过使用其他解决方案之一以及稍后进行浏览器检测来解决此问题).

This works everywhere except some versions of Opera, which I have considered an acceptable loss (I might fix that via the use of one of the other solutions plus browser detection later).

这篇关于如何在不离开页面的情况下提供文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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