I want to preview the image,word or pdf files inside modal popup by Calling a ajax call, on clicking a anchor link and it's also opening the modal popup.
<a href='#' onclick='preview('docId')' title='Preview' data-
target='#previewModal' data-toggle='modal'> Preview file </a>
Ajax code is -
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);
}
});
}
Controller Action Code is -
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);
}
PROBLEM is that, how to handle this action response and Set inside the modal popup Body to render the image or file for preview ?