我的html代码是这样的:
<a href="javascript:;" onclick="showAjaxPdf('{{ $row->file_path }}');"><i class="fa fa-file-pdf-o"></i></a>
我的 javascript 代码是这样的:
function showAjaxPdf(file_path)
{
var file_path = file_path.replace(/\\/g,"/");
//example : file_path = assets/images/myfile.pdf
$.ajax({
type: "POST",
data: 'file_path=' + file_path,
url: "news/test",
success: function(response)
{
$('#test').html(response);
}
});
}
我在 Controller 中的功能测试:
public function postTest(Request $request)
{
$file_path = $request->input('file_path');
return response()->download($file_path);
}
当我点击 pdf 图标时,没有任何反应。
我希望,当点击pdf图标时,出现这样的:
如何保持点击pdf图标时图片显示的样子?
谢谢
最佳答案
我所做的是,编写了两个单独的路由,一个用于验证,一个用于下载。 在一个 ajax 成功后,我触发了 window.open(downloadUrl,'_blank') 在单独的窗口中下载。 这不是要求的方式,但它可以防止任何即将发生的错误,因为验证 url 将对其进行排序
$.ajax({ url: verifyUrl, type: 'get', cache: false, data: null, error: function (err){$('#ajax_loader_div').hide();}, success: function(response) { console.log(response); $('#ajax_loader_div').hide(); if (response.status == 'success') { window.open(downloadUrl,'_blank'); }else if(response.status == 'error') { //show error } } });
关于javascript - laravel pdf文件从ajax请求下载(laravel 5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34519319/