Laravel阻止url在刀片window.location中进行转义

问题描述 投票:0回答:1

我有一个按钮,可以在点击时下载文件,就像这样。

<?php
    $file = public_path('storage\excel.xlsx');
?>
{{$file}} //this prints C:\xampp\htdocs\project1\public\storage\file.xlsx 
<button type="button" class="btn btn-primary btn-add-data" data-toggle="" data-target="" onclick="window.location='{!! $file !!}'"> Download File </button>

当我使用onclick="window.location='C:\xampp\htdocs\project1\public\storage\file.xlsx'"它工作正常。

但是当我使用上面的代码时,它无法抛出错误SyntaxError: malformed hexadecimal character escape sequence

我想这是因为反斜杠导致它尝试转义字符串。我已经尝试使用{!! $file !!}并将其设置为(string)但它仍然无法正常工作。

php html laravel laravel-blade
1个回答
1
投票

你可以在Laravel 5+中使用它

<a href={{ asset($file) }}>Download</a>
© www.soinside.com 2019 - 2024. All rights reserved.