Content-Disposition: attachment; filename
Par PlaceOweb le vendredi, janvier 27 2012, 19:07 - Outils Internet - Lien permanent
Comment faire un téléchargement forcé compatible avec tous les navigateurs (smartphones inclus) ?...
Content-Disposition: attachment; filename=xxx
Ma conclusion
if( preg_match("/.*MSIE .*/",$_SERVER["HTTP_USER_AGENT"]) ) { /** * IE (Internet Explorer) * Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3) * Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0) */ header('Content-Disposition: attachment; filename="' . rawurlencode($row['fichier_nom']) . '"' ); }else{ /** * FF (Firefox) * Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1 * Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0) Gecko/20100101 Firefox/10.0 * * Opera * Opera/9.80 (Windows NT 6.1; U; fr) Presto/2.10.229 Version/11.61 * * Chrome * Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7 * * Safari * Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.52.7 (KHTML, like Gecko) Version/5.1.2 Safari/534.52.7 */ header('Content-Disposition: attachment; filename="' . $row['fichier_nom'] . '"' ); }
RFC
- RFC 2183 Communicating Presentation Information in Internet Messages: The Content-Disposition Header Field
In the extended BNF notation of [RFC 822], the Content-Disposition header field is defined as follows:
disposition := "Content-Disposition" ":" disposition-type *(";" disposition-parm)
disposition-type := "inline" / "attachment" / extension-token ; values are not case-sensitive
disposition-parm := filename-parm / creation-date-parm / modification-date-parm / read-date-parm / size-parm / parameter
filename-parm := "filename" "=" value
creation-date-parm := "creation-date" "=" quoted-date-time
modification-date-parm := "modification-date" "=" quoted-date-time
read-date-parm := "read-date" "=" quoted-date-time
size-parm := "size" "=" 1*DIGIT
quoted-date-time := quoted-string ; contents MUST be an RFC 822 `date-time' ; numeric timezones (+HHMM or -HHMM) MUST be used
Test Cases for HTTP Content-Disposition header field (RFC 6266) and the Encodings defined in RFCs 2047, 2231 and 5987
Comme d'habitude, on constate que le puta.. de Internet Explorer de Windows nous emmerde ! (Toutes versions confondues de la 6 à 9).
Par exemple si votre fichier contient un point virgule (;), alors le nom du fichier est tronqué. attwithquotedsemicolon
Ressources
- PHP header
- How to encode the filename parameter of Content-Disposition header in HTTP?
- Content-Disposition header - forcing SaveAs in browsers
Complément
Avec Android (2.2) et le navigateur par défaut WebKit 3.1 Mozilla/5.0 (Linux; U; Android 2.2; fr-fr; Desire_A8181 Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
, les espaces posent problème : le fichier est renommée en ".bin", "-1.bin", "-2.bin" ...
Et les caractères hors ASCII sont supprimés : "toto%20esticietlà.txt" devient "toto20esticietl.txt"
Alors que depuis la version 4.0, aucun problème de la sorte.
- Android and the HTTP download file headers
- Android - An Open Handset Alliance Project : Issues Content-Disposition
- Android browser/webview bug? Content-Disposition: attachment; filename=“xyz.txt”
- Android HTTP header attachment filename ignored Android tablet will not accept spaces in the filename, the generic solution is remove spaces from the filename before sending the header
- Avoiding content type issues when downloading a file via browser on Android 1. set the ContentType to application/octet-stream 2. put the Content-Disposition filename value in double uotes 3. write the Content-Disposition filename extension in UPPERCASE
Content-Disposition headers in .NET ASP.NET MVC 2 File method will manage the whole download for you, including generating the Content-Disposition header correctly for non-ASCII character filenames.