laravel ÏÂÔع¦Ð§²»¿ÉÓÃÖÐÎÄ
ÔÚʹÓà laravel ¿ª·¢Ê±£¬ÎÒÃÇ¿ÉÄÜ»áÓöµ½ÏÂÔع¦Ð§²»¿É¹»×¼È·´¦Öóͷ£ÖÐÎÄÎļþÃûµÄÎÊÌâ¡£Õâ¸öÎÊÌâÖ÷ÒªÊÇÓÉÓÚÔÚÎļþÃûÖаüÀ¨ÓÐÖÐÎÄ×Ö·ûËùµ¼Öµġ£Òª½â¾öÕâ¸öÎÊÌ⣬ÎÒÃÇÐèÒª¶ÔÎļþÃû¾ÙÐÐ url ±àÂë¡£
ÔÚ PHP ÖУ¬¿ÉÒÔʹÓà urlencode() º¯ÊýÀ´¶ÔÎļþÃû¾ÙÐбàÂë¡£¿ÉÊÇ£¬ÔÚ Laravel ÖÐÎÒÃÇ¿ÉÒÔʹÓÃÄÚÖÃµÄ Str::slug() ÒªÁìÀ´Íê³ÉÕâ¸öʹÃü¡£
Str::slug() ÒªÁì¿ÉÒÔ½«Ò»¸ö×Ö·û´®×ª»¯Îª URL ÓÑºÃµÄ “slug” ÃûÌ᣿ÉÊÇ£¬ÎÒÃÇ¿ÉÒÔÑ¡Ôñ²»´«ÈëµÚ¶þ¸ö²ÎÊý£¬ÒÔ±£´æÔÓÐ×Ö·û£¬Ö»¾ÙÐÐ URL ±àÂë¡£
ÏÂÃæÊÇÒ»¸öʾÀý´úÂ룺
public function downloadFile($filename) { $fullPath = storage_path('app/download/' . $filename); $headers = [ 'Content-Type' => 'application/octet-stream', ]; $escapedFilename = Str::slug($filename, ''); return response()->download($fullPath, $escapedFilename, $headers); }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃÁË Str::slug() ÒªÁ콫ÎļþÃû¾ÙÐÐÁ˱àÂ룬²¢½«±àÂëºóµÄ×Ö·û´®×÷ΪµÚ¶þ¸ö²ÎÊýת´ï¸ø download() ÒªÁì¡£
ͨ¹ýÕâÖÖ·½·¨£¬ÎÒÃÇ¿ÉÒÔÈ·±£ Laravel ÔÚ´¦Öóͷ£ÏÂÔØÎļþʱÄܹ»×¼È·´¦Öóͷ£°üÀ¨ÖÐÎÄ×Ö·ûµÄÎļþÃû¡£
ÒÔÉϾÍÊÇlaravel ÏÂÔع¦Ð§²»¿ÉÓÃÖÐÎĵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡