【发布时间】:2021-01-21 19:50:49
【问题描述】:
我正在尝试显示存储在数据库中的 blob 图像,我没有收到任何错误但图像没有显示,我只是获得默认的“无图像”图标。这是我的代码:
<script>
function showEmpimg(str) {
var xhttp;
if (str == "") {
document.getElementById("user-id").innerHTML = "";
return;
}
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("face").innerHTML = this.responseText;
}
};
xhttp.open("POST", "getimage.php?q="+str, true);
xhttp.send();
}
</script>
</head>
<body>
<div id="face" class="face">
</div>
<input type="text" class="form-control" id="user-id" placeholder="ID" name="emp_id" onchange="showEmpimg(this.value)" required maxlength="6" />
还有php文件:
<?php
$db = mysqli_connect("localhost","root","test1","dar");
$sql = "SELECT emp_img FROM employees WHERE emp_id LIKE 'q'";
$sth = $db->query($sql);
$result=mysqli_fetch_array($sth);
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['emp_img'] ).'"/>';
?>
有什么想法吗?
【问题讨论】:
-
$result['emp_img']的内容是什么? -
我应该是一个 BLOB .jpg 文件。从数据库。执行后检查代码时,img src 显示“data:image/jpeg;base64”,但其余部分不显示。
标签: javascript php database image xhtml