在浏览器上显示保存在gridfs中的pdf文件的预览

时间:2018-09-17 13:27:23

标签: php jquery mongodb symfony gridfs

我正在使用gridfs将文件存储在MongoDB中。尝试在浏览器中显示大的pdf文件时,我遇到了一个问题,该文件保存在gridfs中。大文件不会显示在浏览器中,但小文件会显示在浏览器中。

这是服务代码

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;
}

这是前端代码

Ajax(path, data).success(function (result) {
                        $("#pdfDiv").html('<iframe style="width:100%;height:500px;" src="data:application/pdf;base64,' + result.data + '"></iframe>');
                });

代码是否有任何问题?如何在前端显示大文件?

1 个答案:

答案 0 :(得分:1)

这是我的建议,没有在新标签页中打开, http://answerexpress.blogspot.com/2018/09/show-preview-of-pdf-file-saved-in.html