大家好,我是 Laravel 和 Reactjs 的新手,我有一个问题,我尝试从服务器下载文件到浏览器。我的请求没问题,但我想要的文件不可读(如果我右键单击浏览器网络->预览,我发现符号和字符不可读),文件也未下载。我使用 Visual Studio Code 在 Windows 中进行编码。
下载控制器:
public function download()
{
$file = public_path()."/file.pdf";
return response()->download($file);
}
Run Code Online (Sandbox Code Playgroud)
路线/api.php:
Route::get('download','Api\DownloadController@download');
Run Code Online (Sandbox Code Playgroud)
在文件Js中:
import React, { Component } from 'react';
import axios from 'axios';
export default class download extends Component{
constructor () {
super();
}
componentDidMount() {
axios.get(`http://127.0.0.1:8000/api/download`)
.then((response) => {
console.log('hello');
});
}
render() {
return (
<div>
<button onClick={this.componentDidMount.bind(this)} className="btn btn-primary">Download</button>
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)