[英]How to detect when cancel is clicked on save file dialog? (Javascript)
我有一个php-script来更新相应文件的下载计数器(它发送带有文件的标头)。 但是,如果我单击“保存文件”对话框上的“取消”按钮,则计数器也会增加。 好像错了
我的想法是,如果单击取消按钮,则发送ajax请求和减量计数器。 但是如何检测取消按钮的点击呢?
您应该尝试使用Javascript跟踪文件下载进度,并在完成100%下载时增加增量
我解决了我的问题,例如使用PHP中的fread
下载部分文件。
看起来像这样:
// send file (it shows save file dialog)
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($filepath));
header('Content-Disposition: filename='.$filename);
flush();
$file = fopen($filepath, "r");
while(!feof($file))
{
// send the current file part to the browser
print fread($file, round($download_rate * 1024));
// flush the content to the browser
flush();
// sleep one second
sleep(1);
}
// if all are right and file was downloaded
if (feof($file)) {
update_current_counter_or_insert_new_one_in_DATABASE();
}
fclose($file);
这个链接可以帮助我解决(php.net readfile文章)。 因此,它或多或少对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.