0

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 ?

Techno
  • 55
  • 11
  • I think that's not possible for word & pdf docs, but the image you can get the url and set it as src to an image element you create with js – Med.ZAIRI Apr 24 '18 at 18:26
  • what should i do? i don't want to view in browser or download the file . @Med.ZAIRI – Techno Apr 24 '18 at 18:33
  • for pdf files I think this could help you [http://mozilla.github.io/pdf.js/](http://mozilla.github.io/pdf.js/) – Med.ZAIRI Apr 24 '18 at 23:16
  • 1
    also check this: https://stackoverflow.com/questions/14690000/embed-a-pdf-in-html5 – Med.ZAIRI Apr 24 '18 at 23:24
  • Thanks to @Med.ZAIRI .. I implemented by . Successfully previewing the files/Images in Modal popup, but another concern is to prevent the user to print or download the file? – Techno Apr 25 '18 at 11:24

0 Answers0