简体   繁体   中英

How to use the content-disposition header to set a downloaded UTF-8 filename?

I am storing my files on Amazon Cloud Drive. And there are option for setting response-content-disposition to direct links from Amazon.

From the API:

response-content-disposition : (Optional) If specified, the content-disposition of the response will be set to this value

I need to set it download mp3 files. I don't need to play it on browser.

And I want to set downloaded file name: Эльбрус Джанмирзоев - Кавказская любовь.mp3

I tried many ways... Urlencode... Urlencode + http build query etc...

But now the result is: Эльбрус+Джанмирзоев+-+Кавказская+любовь.mp3 when downloading files.

And here is my php code:

<?php
    function file_generate_download_link($filename = false, $force = true)
    {
            $json['tempLink'] = 'https://content-na.drive.amazonaws.com/cdproxy/templink/iTwUqoQ3cJXOpj6T8SCDmKwtF37TAENiSLQzy7pTSbkFfJttb';
            $url = $json['tempLink'];
            if($force)
            {
                $data['download'] = 'true';
            }
            if($filename)
            {               
                $data['response-content-disposition'] = ' attachment; filename*=UTF-8\'\''.urlencode($filename).'';
            }
            if(isset($data)) {
                $data = http_build_query($data);
                $url .= '?' . $data;
            }
            return $url;

    }

    echo file_generate_download_link($filename = 'Эльбрус Джанмирзоев - Кавказская любовь.mp3');
?>

This code returns me this link:

https://content-na.drive.amazonaws.com/cdproxy/templink/iTwUqoQ3cJXOpj6T8SCDmKwtF37TAENiSLQzy7pTSbkFfJttb?download=true&response-content-disposition=+attachment%3B+filename%2A%3DUTF-8%27%27%25D0%25AD%25D0%25BB%25D1%258C%25D0%25B1%25D1%2580%25D1%2583%25D1%2581%2B%25D0%2594%25D0%25B6%25D0%25B0%25D0%25BD%25D0%25BC%25D0%25B8%25D1%2580%25D0%25B7%25D0%25BE%25D0%25B5%25D0%25B2%2B-%2B%25D0%259A%25D0%25B0%25D0%25B2%25D0%25BA%25D0%25B0%25D0%25B7%25D1%2581%25D0%25BA%25D0%25B0%25D1%258F%2B%25D0%25BB%25D1%258E%25D0%25B1%25D0%25BE%25D0%25B2%25D1%258C.mp3

If I enter this link Chrome saves this file with this name:

Эльбрус+Джанмирзоев+-+Кавказская+любовь.mp3

But I need excatly save file with this name:

Эльбрус Джанмирзоев - Кавказская любовь.mp3

Where is my mistake?

Gotcha! I need to use rawurlencode function!

So the right code is:

<?php
    function file_generate_download_link($filename = false, $force = true)
    {
            $json['tempLink'] = 'https://content-na.drive.amazonaws.com/cdproxy/templink/iTwUqoQ3cJXOpj6T8SCDmKwtF37TAENiSLQzy7pTSbkFfJttb';
            $url = $json['tempLink'];
            if($force)
            {
                $data['download'] = 'true';
            }
            if($filename)
            {               
                $data['response-content-disposition'] = ' attachment; filename*=UTF-8\'\''.rawurlencode($filename).'';
            }
            if(isset($data)) {
                $data = http_build_query($data);
                $url .= '?' . $data;
            }
            return $url;

    }

    echo file_generate_download_link($filename = 'Эльбрус Джанмирзоев - Кавказская любовь.mp3');
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM