show preview of pdf file saved in gridfs on browser




Hi i'm using gridfs to store file in mongodb, I'm facing a problem when i try to show the large pdf file in browser, which is saved in gridfs, large files doesn't showing in browser, but small files showing. this is my code, service
public function getIpFileByFileId() {
    $request = $this->request;
    $dm = $this->container->get('doctrine_mongodb')->getManager('patient');
    $id = $request->get('fileId');
    //get doc
    $docIpMapping = $dm->getRepository('PatientDocumentsBundle:IPDocuments')->findOneBy([
        'id' => $id
    ]);
    $base64 = (base64_encode($docIpMapping->getFile()->getBytes()));
    $response['data'] = $base64;
    $response['msg'] = 'success';
    return $response;
}
in front end
Ajax(path, data).success(function (result) {
                        $("#pdfDiv").html('<iframe style="width:100%;height:500px;" src="data:application/pdf;base64,' + result.data + '"></iframe>');
                });
Is my code have any problem? how to display large files in front end?

Ans:  in your code from service returning base64 data, instead of base64 return $docIpMapping->getFile()->getBytes()
in controller return as 
return new Response($response['data'], 200, array(
            'Content-Type' => 'application/pdf'));
and in front end
var url = "{{ path('get_ip_file_by_id',{'fileId' : '_ID_'})}}".replace('_ID_', fileId);
                    $("#pdfDiv").html('<iframe style="width:100%;height:500px;" src="' + url + '"></iframe>');
here you can load in iframe without loading to new page.

Comments

Popular posts from this blog

Install canon LBP2900 driver in linux

python subprocess become idle after a while