我有一个按钮,可以在点击时下载文件,就像这样。
<?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)
但它仍然无法正常工作。
你可以在Laravel 5+中使用它
<a href={{ asset($file) }}>Download</a>