从数据库显示 Blob 图像

问题描述 投票:0回答:1

我真的要疯了。我无法弄清楚下面的代码中的错误在哪里,其目的是从数据库中检索图像并将其显示在 id 为“img1”的框架中。我已经尝试了各种方法,但我得到的只是一个没有转换成图像的字符串。有什么建议吗?

<?php

//file foto.php

include './admin/conn.php';

if (!empty($_POST['photo'])) {

 $Id = $_POST['photo'];

  $query=$conny->query("SELECT * from picture where cf ='$Id'");
        while($row=$query->fetch_array()) {
        
    echo $row["image"];
         
        }
} else {
        echo $searchErr = "Errore nell'estrapolazione del dato IN/OUT";
    } 
    
?>
//file index.php

 $.ajax( {
                                method: 'POST',
                                url: 'foto.php',
                                async: false,
                                data:{
                                    photo: data14      
                                    },
                                
                                success: function (result) {
                                    if (result != null && result != "") {
                                    
                                     
                        alert(result);   //<<<<<< (i get a JFIF string)
                                
        
document.getElementById("img1").src= '<img src="data:image/jpeg;base64, base64_encode('+result+') width="150px" height="150px" />';    //<<<<< (i get an empty frame)
                                                    
                                }}
                            })
javascript php mysql ajax
1个回答
0
投票

该字符串就是图像本身,您只是缺少一些如何显示它的细节。

你说

.src=
;接下来的内容应该是您希望将 src 属性设置为的内容,而不是整个 img 标签。但你不能设置宽度和高度;如果 img 标签的其余部分不是您想要的方式,您可能需要修改代码来替换图像标签,而不是仅仅设置 src 属性。

你似乎试图在你的javascript代码中调用base64_encode php函数;那不是那样的。使用 btoa javascript 函数:

document.getElementById("img1").src = 'data:image/jpeg;base64, ' + btoa(result)
© www.soinside.com 2019 - 2024. All rights reserved.