检测文件是否已提供给浏览器?..有点
我有一个按钮,它将 window.location 设置为 php 文件,该文件生成一个提要,然后下载该提要。但是,由于文件大小因放入源中的数据而异,因此有时从单击按钮到弹出文件对话框可能需要一段时间。
我希望能够做的是单击按钮并显示loading.gif,直到对话框/文件完成。
任何想法都会很酷!
干杯
I have a button which sets window.location to a php file which generates a feed which is then downloaded. However, as the files vary in size due to what data is put into the feed it can sometimes take a while from the click of the button to the file dialog popping up.
What I would like to be able to do is click the button and display a loading.gif until the dialog / file is complete.
Any ideas would be cool!
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我不太确定为什么你需要检查文件的大小?如果您使用 ajax 动态执行 get/post,并且您所做的只是尝试在发生这种情况时显示加载图标,则抛出异步活动指示器相当简单。例如,使用 jquery:
上面的代码设置了一个 id 为“loading”的 DOM 对象,以在启动和停止任何异步请求时显示和隐藏。 .load(url) 将 url 的内容加载到 div #feeds 中。如果您使用 php 设置 content-disposition:attachment 标头,它将自动启动文件下载窗口,即使它已异步加载到 div 中。当然,没有jquery也是可能的,只有一堆浏览器兼容性javascript,它不像简单地订阅ajaxStart和ajaxStop事件来显示和隐藏加载的img那么简单。
乔什
I'm not really sure why you need to check the size of the file at all? If you use ajax to dynamically do the get/post, and all you are doing is trying to show a loading icon while this is happening, its fairly simple to throw up an asynchronous activity indicator. For instance, with jquery:
The above code sets a DOM object with id "loading" to show and hide when any asynchronous request has been initiated and stopped. .load(url) loads the content of the url into the div #feeds. If you are setting the content-disposition: attachment header with php, it will automatically initiate a file download window, even though it has been loaded asynchronously into a div. This is also possible without jquery of course, there's just a bunch of browser compatibility javascript and its not as easy as simply subscribing to the ajaxStart and ajaxStop events to show and hide your loading img.
Josh
在设置 window.location 之前,您可以使用 gif 显示隐藏的 div
before setting window.location you could display a hidden div with your gif
这是老派的做法,但这可以通过服务器推送轻松完成,
只需调整要发送的数据的内容类型即可。 $separator 可以是任何值,只要它不出现在正在发送的数据中即可。
It's old school, but this can be done very easily with server push
Just adjust the content-types for the data you're sending. $separator can be any value so long as it does not appear in the data being sent.
我过去使用过的方法是这样的...
编辑:加载页面应使用 javascript 重定向到目标页面(将 window.location 设置为查询字符串中提供的 URL)。这是很重要的一点,因为如果您在服务器端重定向,则不会显示加载页面。
编辑2:如果您的加载页面是一个php文件,您可以检查要下载的文件的大小并向用户显示估计的下载时间(以及动画“加载”gif),或者诸如此类。
A method I've used in the past works like this...
EDIT: The loading page should redirect to the destination page using javascript (set window.location to the URL provided in the querystring). This is an important point, because if you redirect on the server-side the loading page won't get displayed.
EDIT 2: If your loading page is a php file, you can check the to-be-downloaded file's size and display an estimated download time to the user (along with an animated "loading" gif), or whatnot.
只需让 RSS 生成脚本显示您想要的图像(输出图像的 HTML,然后刷新输出缓冲区并开始数据生成)。在数据生成结束时执行以下操作:
仅此而已。
Just make the RSS generating script show image you want (output image's HTML, then flush output buffer and start data generation). At the end of the data generation do:
That's all.
您必须使用 AJAX 与服务器通信才能发现您正在下载的文件的确切大小。然后你就有了可以测试的东西。仅从客户端无法获知预期有效负载的大小。
You will have to use AJAX to communicate to the server to discover the exact size of the file you are downloading. Then you have something to test against. There is no way to know the size of the expected payload from the client side only.
您是否可以使用 iframe 加载 feed,然后使用间隔检查 iframe/文档的就绪状态?因此,该过程将是:
一个缺点是,对于大多数浏览器来说,PHP 文件需要位于同一域中(在 IE 上,您只需检查 iframe 的 readyState 属性)。
Is it possible for you to use an iframe to load the feed and then check the readyState of the iframe/document using an interval? So the process would be:
One downside is, for most browsers the PHP file would need to be on the same domain (on IE you can just check the readyState property of the iframe).