How to encode the filename parameter of Content-Disposition header in HTTP?

前端 未结 18 1793
北荒
北荒 2020-11-21 06:15

Web applications that want to force a resource to be downloaded rather than directly rendered in a Web browser issue a Content-Disposition hea

18条回答
  •  梦如初夏
    2020-11-21 07:04

    PHP framework Symfony 4 has $filenameFallback in HeaderUtils::makeDisposition. You can look into this function for details - it is similar to the answers above.

    Usage example:

    $filenameFallback = preg_replace('#^.*\.#', md5($filename) . '.', $filename);
    $disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename, $filenameFallback);
    $response->headers->set('Content-Disposition', $disposition);
    

提交回复
热议问题