php - 如何从 Laravel 应用程序的服务器文件夹中预览 pdf、Word 或图像文件?
问题描述
我想通过调用 ajax 调用来预览模式弹出窗口中的图像、word 或 pdf 文件,单击锚链接,它也会打开模式弹出窗口。
<a href='#' onclick='preview('docId')' title='Preview' data-
target='#previewModal' data-toggle='modal'> Preview file </a>
Ajax 代码是 -
function preview(docid)
{
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
$.ajax({
url : 'docs/viewDoc' ,
type : 'POST',
data: {_token: CSRF_TOKEN, id:docid},
dataType : 'plain/text OR JSON', ???????
success : function(d){
console.log(d.data);
$('#loader').hide();
$('#previewModal div.modal-body').html(d.data);
},
error : function(d){
alert('Error - '+ d);
console.log(d);
}
});
}
控制器操作代码是 -
public function viewDoc(Request $request)
{
$id = $_POST['id'];
$conditions =['docId'=>$id];
$file_info = Docs::select('path','docType','Title')->where($conditions)-
>first();
$file= File::get($file_info->path);
$response = Response::make($file, 200);
$response->header('Content-Type', 'application/pdf');
return response()->view('admin.docs.test')->with('response',$response);
OR ???? how
return response()->view('admin.docs.test',$response,200) ;
OR ????? how
//return response()->json($response,JSON_UNESCAPED_UNICODE);
}
问题是,如何处理此动作响应并在模态弹出主体内设置以渲染图像或文件以进行预览?
解决方案
推荐阅读
- linux - 本地和 QA 上的 CSV 导出编码不匹配
- python - Python,用于总结的每个句子的词频
- javascript - 如何将包含 json 数据的文本文件转换为订购了所有 json 有效负载的 json 文件?
- r - 有没有办法在 JAGS 中获取和存储矩阵元素的位置?
- python - Scipy.integrate.odeint 在不连续函数上的意外行为
- json - 在 Json 配置命令中添加新行
- linux - 为什么使用部署管理器模板创建资源时不需要服务帐户用户角色(roles/iam.serviceAccountUser)?
- oracle - Sqoop 导出 Oracle : java.lang.OutOfMemoryError: Java heap space
- mysql - 如何在单个 mysql 查询中获取当前日期、当前星期、当前月份、当前年份销售额总和?
- mongodb - What is the correct way to use date in Mongoose query?