首页 > 解决方案 > 无法使用 php 和 Jquery 打开 PDF 文件

问题描述

在我的情况下,无法显示所有 pdf 文件,有些 pdf 可以在浏览器上查看,有些无法查看,试图回显 pdf 文件但不能一直工作,顺便说一下,我所有上传的文件都上传到了MySql 数据库.. 我不使用文件夹来移动上传的文件.. 请参阅我的 jquery 代码和 php 代码

 $.ajax({
                                    url:'fetchAllAttachenemts.php',
                                    method:'POST',
                                    data:{DispID:DispID},
                                    success:function(data)
                                    {               
                                        if ( data == '')
                                        {
                                            $('#AttachementTable tbody' ).html( ' <tr> <td></td><td>No Files Were Uploaded</td><td></td></tr>');

                                        }
                                        else
                                        {
                                            $('#AttachementTable tbody' ).html(data);
                                        }
                                    },
                                    error:function(data)
                                    {
                                        alert(data);
                                    }
                                });




include 'connect.php';
global $con;
if (isset($_GET['id']))
{
    $ID = $_GET['id'];   
    $stat = $con->prepare("SELECT * FROM dispatchattachmnt WHERE ID = ? ");
    $stat->bindParam(1,$ID);
    $stat->execute();
    $row  = $stat->fetch();
    $file = $row['fileName']; 
    $filename = $row['fileName']; 
    header('Content-type: '.$row['fileMimi']);    // = header('Content-Type: application/pdf');
    header('Content-Disposition: inline; filename="'.$filename.'"'); 
    header('Content-Transfer-Encoding: binary'); 
    header('Accept-Ranges: bytes'); 
    //echo (@readfile($file));
    echo $row['data'] ; // from blob in mysql 
}
else 
{
    echo "NOT SET";
}

她是我获取附件的代码

session_start();
include 'connect.php';
global $con;
$outpout='';
$ID= "";
if (isset($_POST['DispID']))
{
    if($_POST['DispID'] !="")
    {
        $id = $_POST['DispID'];
        $query= "SELECT * FROM dispatchattachmnt WHERE dispID = ?";
        $stmt = $con->prepare($query);
        $stmt->execute(array($id));
        $result = $stmt->fetchAll();

        $count = $stmt->rowCount();
        if ($count >0)
        {
            foreach ($result as $row)
            {
                $outpout .= '<tr>';
                $outpout .= '<td  id = "'.$row['ID'].'"   style="cursor:pointer"> <span class="modifyAttacheDesc"> '.$row['disc'].' </span></td> ';
                $ID = trim($row['ID']);
                $outpout .= '<td id = "'.$row['ID'].'"   class="openAtt"> <a  target="_blank" href="viewAttachement.php?id='.$row['ID'].'">'.$row['fileName'].'</a></td>';
                $outpout .= '<td id = "'.$row['ID'].'" ><i  id ="'.$row['ID'].'" class="fas fa-trash-alt task-icon2" </i></td>';   
                $outpout .='</tr>';
            }

            echo $outpout;
        }
    }
}```

标签: phpjquery

解决方案


推荐阅读