Internet Explorer 让我非常头疼......
我想要做的是 - 创建一个按钮,单击该按钮即可将 .csv 文件下载到客户端。此 .csv 文件包含存储在我在页面上生成的结果表之一中的信息。
在创建此按钮之前,我将调用内部函数来根据当前显示的表格创建 .csv 文件。我在服务器上创建了这个 .csv 文件。为了以防万一,我将在此处包含此功能,但我认为它没有任何帮助。就像我说的,在创建按钮之前我创建了这个文件。
/*
/ Creates a .csv file including all the data stored in $records
/ @table_headers - array storing the table headers corresponding to $records
/ @records - two dimensional array storing the records that will be written into the .csv file
/ @filename - string storing the name of the .csv file created by this from
*/
public function export_table_csv( $table_headers, $records, $filename )
{
// Open the $filename and store the handle to it in $csv_file
$csv_file = fopen( "temp/" . $filename, "w" );
// Write the $table_headers into $csv_file as a first row
fputcsv( $csv_file, $table_headers );
// Iterate through $records and write each record as one line separated with commas to $csv_file
foreach ( $records as $row )
fputcsv( $csv_file, $row );
// Close $csv_file
fclose( $csv_file );
} // end export_table_csv()
我的工作正常。我得到了“导出”按钮,并且正在使用其 onClick() 事件,其中我使用了一行:
window.open( 'temp/' . $export_filename );
现在,它可以在除 IE 之外的所有浏览器中正常工作。文件仍然会被下载,但是当我对页面上显示的表格执行一些过滤时(每当应用新过滤器时,页面就会重新加载),然后再次按“导出”按钮,它会以某种方式下载旧版本应用了旧过滤器(而不是当前过滤器)的 .csv 文件,即使每次应用新过滤器并重新加载页面时都会重写此 .csv 文件。
就好像我导出的 .csv 文件存储在 IE 的缓存或其他东西中...这真的很烦人,因为导出在所有其他浏览器中都可以正常工作...Chrome 和 FF 总是下载最新版本的来自服务器的文件,IE 随机更新文件,有时只有在我使用不同的过滤器提交页面几次后...
我没有包含太多代码行,因为我宁愿认为我只是缺少某种元标记或代码中的某些内容,而不是在我已经编写的行中存在逻辑错误。
我真的对此感到困惑,至少可以说很恼火...我现在真的开始不喜欢 IE...
非常感谢有关此事的任何建议。
最佳答案
您可以使用“缓存破坏程序”来阻止 IE 缓存资源。
如果您将 GET 参数(其值在每次加载页面时都会更改)添加到 URL,IE(或更确切地说:任何浏览器)会认为这是一个不同的文件,因此请执行以下操作:
window.open( 'temp/" . $export_filename . "?cachebuster=" . uniqid(true) . "' );
如果每次单击时该值都需要更改(而不是在页面加载时):
window.open( 'temp/" . $export_filename . "?cachebuster="' + Math.random() );
关于php - 如何在 IE (PHP) 中下载服务器文件的更新版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11030416/