首页 > 解决方案 > 如何从 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);   
}

问题是,如何处理此动作响应并在模态弹出主体内设置以渲染图像或文件以进行预览?

标签: phpjqueryajaxlaravel-5.6

解决方案


推荐阅读