【问题标题】:How to download the updated version of a server file in IE (PHP)?如何在 IE (PHP) 中下载服务器文件的更新版本?
【发布时间】:2012-06-14 09:39:04
【问题描述】:

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了......

感谢您对此事提出的任何建议。

【问题讨论】:

    标签: php internet-explorer caching csv download


    【解决方案1】:

    您可以使用“缓存破坏器”来防止 IE 缓存资源。

    如果你向 URL 添加一个 GET 参数(每次加载页面时值都会改变),IE(或者更确切地说:任何浏览器)会认为它是一个不同的文件,所以请执行以下操作:

    window.open( 'temp/" . $export_filename . "?cachebuster=" . uniqid(true) . "' );
    

    如果每次点击时值都需要改变(不是在页面加载时):

    window.open( 'temp/" . $export_filename . "?cachebuster="' + Math.random() );
    

    【讨论】:

    • 非常感谢!像魅力一样工作。 +3
    猜你喜欢
    • 2014-03-22
    • 2017-08-17
    • 2012-09-15
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    相关资源
    最近更新 更多