简体   繁体   English

尽管浏览器预取/缓存,如何判断文件是否真的被下载和保存?

[英]How to tell if a file was really downloaded and saved, despite browser prefetch/cache?

I am creating a script to keep download statistics, but I would only like to update the stats if the file was ACTUALLY downloaded.我正在创建一个脚本来保存下载统计信息,但我只想在文件实际下载的情况下更新统计信息。

The problem is that browsers (eg, Explorer 9, Chrome, ...) automatically start downloading the file while the 'save/open' dialog is still open.问题是浏览器(例如,Explorer 9、Chrome 等)会在“保存/打开”对话框仍然打开时自动开始下载文件。 This means the file can completely download (into a browser specified temp directory I assume) before the user has specified where to save it (or while Explorer still shows the Yellow warning bar).这意味着文件可以在用户指定保存位置之前完全下载(到浏览器指定的临时目录中)(或者当资源管理器仍然显示黄色警告栏时)。

The user may decide to cancel the save, even though the file was successfully downloaded.即使文件已成功下载,用户也可以决定取消保存。 How can I tell browsers not to automatically start downloading?如何告诉浏览器不要自动开始下载? Some HTTP header perhaps?一些 HTTP header 也许? Any other solutions?还有其他解决方案吗?

You can't do what you are wanting to do.你不能做你想做的事。 It is impossible, at least in the way you are asking.这是不可能的,至少在你问的方式上。

What you can do is put some code in the installer that reaches out to your server when it runs, and your script can keep track of it then.可以做的是在安装程序中放入一些代码,以便在服务器运行时访问您的服务器,然后您的脚本可以跟踪它。 This is commonly done, but is certainly not foolproof.这通常是这样做的,但肯定不是万无一失的。 Many people download software and install offline.许多人下载软件并离线安装。 (Particularly, dial-up users, which are still out there by the thousands.) Also, there is no guarantee that the installer won't be blocked by firewall software or something. (特别是拨号用户,仍然有成千上万的用户。)此外,不能保证安装程序不会被防火墙软件或其他东西阻止。

So this all depends on how bad you need to know when it was installed.所以这一切都取决于你需要知道什么时候安装它。 If it is critical, then your installer needs to verify with the server before installing.如果它很重要,那么您的安装程序需要在安装之前与服务器进行验证。 Of course this can be hacked around, and there is nothing you can do about it.当然,这可以被黑客入侵,您对此无能为力。 But, I suspect it will work for your purposes.但是,我怀疑它会为你的目的工作。

Also, don't forget about multiple installs per download.另外,不要忘记每次下载要多次安装。

I don't think you can see this.我不认为你能看到这个。 You'll have to let someone pay for the software before sending it to the client.在将软件发送给客户之前,您必须让某人为该软件付费。

And I suggest you let the site remember the purchase, so if something went wrong during the download, a user can retry, without having to pay again (or contacting an administrator).我建议您让网站记住购买,因此如果在下载过程中出现问题,用户可以重试,而无需再次付费(或联系管理员)。

Well, having some sort of intermediate page to confirm "yes I want to download this" may mostly eliminate canceled downloads.好吧,有某种中间页面来确认“是的,我想下载这个”可能主要消除取消的下载。

Now, I'm not sure this is entirely possible, but you may try to serve the file via PHP, instead of the typical download from Apache.现在,我不确定这是否完全有可能,但您可以尝试通过 PHP 提供文件,而不是从 Apache 的典型下载。 In PHP, you can use readfile to serve a file from the PHP script.在 PHP 中,您可以使用readfile来提供来自 PHP 脚本的文件。 So, you may be able to call readfile, flush, and determine the number of bytes served.因此,您可以调用 readfile、flush 并确定所服务的字节数。 If that doesn't equal the number of bytes in the file, it was interrupted.如果这不等于文件中的字节数,则它被中断。 I'm not sure if readfile works like that, but its worth experimenting with.我不确定 readfile 是否可以这样工作,但值得一试。

Note, that still won't fix what Marcel mentioned, about hitting 'Cancel' before the tempfile download completes.请注意,这仍然无法解决 Marcel 提到的关于在临时文件下载完成之前点击“取消”的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM