Harts
Harts

Reputation: 4093

IE8 download filename become garbage if it's a Japanese name

I have set up my database collation to utf8_unicode_ci, and on my CakePHP database setup.

class DATABASE_CONFIG {
    public $default = array(
        'datasource' => 'Database/Mysql',
        'persistent' => false,
        'host' => 'xxx',
        'login' => 'xxx',
        'password' => 'xxx',
        'database' => 'thisisdatabase',
        'prefix' => '',
        'encoding' => 'utf8',
    );
}

I set up download binary file like this:

$this->viewClass = 'Media';
$params = array(
    'id'        => $download_list[0],
    'download'  => true,
    'extension' => $download_file_ext,
    'path'      => $fileDownloadPath . DS,
);
$this->set($params);

When the user uploads a file, the filename will be recorded in the database as unicode-utf8. When I download through Chrome, FF, it comes out fine (the Japanese name will still be intact). But when I download through IE, the filename is garbage. Although the file itself is fine (I can open it, etc).

Anyone know how to solve this?

Upvotes: 0

Views: 1292

Answers (1)

Saket Patel
Saket Patel

Reputation: 6683

I am not sure about how CakePHP handles this scenario, but IE has problems with sending Unicode letters in filename header for content-disposition: attachment. So to fix this in IE, we need to url-encode the filename before setting it into the header:

$filename = 'someunicodefilename.txt';
if(// IE) {
   $filename = urlencode($filename);
}
header('Content-Disposition: attachment; filename="' . $filename . '"');

So I am not sure how to achieve this in CakePHP, but you can try this:

$this->viewClass = 'Media';
$filename = 'someunicodefilename.txt';
if(// IE) {
   $filename = urlencode($filename);
}

$params = array(
    'id'        => $download_list[0],
    'name'      => $filename,
    'download'  => true,
    'extension' => $download_file_ext,
    'path'      => $fileDownloadPath . DS,
);
$this->set($params);

Upvotes: 5

Related Questions