[HTTP] Content-Disposition 中文解碼支援所有瀏覽器

Intro

非英文語系檔名下載要支援多瀏覽器需要特別處理一下。

目標是達到Google Chrome, FireFox, Edge, IE以及更多的瀏覽器皆能支援。

Content-Disposition – HTTP | MDN


解決方法

使用filename*並遵循RFC 5987編碼標準即可,格式範例:

Content-Disposition: attachment; filename=%E4%B8%AD%E6%96%87.txt; filename*=UTF-8''%E4%B8%AD%E6%96%87.txt;

PHP sample code:

$filename = rawurlencode('中文檔名File測試');
        header("Content-Disposition: attachment; filename={$filename}{$extension}; filename*=UTF-8''{$filename}{$extension};");

Leave a Reply

Your email address will not be published. Required fields are marked *