检查用户是否接受 JavaScript 中的文件下载
如何检查用户是否接受 JavaScript 中的文件下载。示例:如果网站弹出一个下载链接,并且网络浏览器要求用户下载文件,我如何在该页面上确定用户是否接受下载?
How do I check if the user accepted a file download in JavaScript. Example: If the site pops up a download link, and the web browser asks the user to download the file, how do I determine on that page if the user accepted the download or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JavaScript 将无法访问该信息。一般来说,Web 浏览器内的 JavaScript 仅限于与 DOM 进行简单交互。
您也许可以在服务器端执行一些操作来记录下载流的开始,但如 @Pointy 和 @Marcel 在另一个答案的评论中指出,这可能非常棘手。在这种情况下,您可以使用 AJAX 或长轮询等,近乎实时。
JavaScript will not have access to that information. In general, JavaScript inside web browsers is limited to simply interact with the DOM.
You may be able to do something on the server-side that logs the start of the download stream, but as @Pointy and @Marcel noted in comments to another answer, this could be quite tricky. In such a case, you would then be able to ask the server for this information using AJAX, or long polling, etc, in near real-time.
您必须通过 AJAX 调用检查 Web 服务器日志文件来间接完成此操作。这将要求您编写一些服务器端代码(并且您必须有权访问日志文件)。
还有第二种方法,那就是通过服务器端程序流式传输文件。这样,您可以在文件开始传输时设置一个标志,并通过 AJAX 调用检索该标志。
You will have to do it indirectly by checking the web server log file via an AJAX call. This will require you to write some server side code (and you must have access to the log files).
There's a second way of doing it, and that's to stream the file through a server side program. That way you can set a flag when the file has started to stream and retrieve this flag via an AJAX call.