我的控制器中有以下方法,当通过POST调用路由“ / download /”时触发:
public function download(Request $request) {
return response()->streamDownload( function () use ( $request ) {
return $request->content;
}, 'page.html' );
}
我正在通过javascript应用程序通过AJAX调用此路由。
问题是文件下载不起作用。响应为空。但是,响应代码为200,没有错误...
怎么了?
谢谢
答案 0 :(得分:0)
好的,我做错了。这是我的解决方案:
// @param url : the url of the file on server
function downloadHTML (url) {
url = new URL(url);
return axios({
// I've created a GET API route on my server using the pathname of the file url, which returns the content of the file
url: url.origin + '/api' + url.pathname,
method: 'GET',
responseType: 'blob',
}).then((response) => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'file.pdf'); //or any other extension
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
});
}