我有一个存储在数据库数据类型longblob上的图像,并希望将其显示在页面上。
我正在使用php访问数据库,然后创建一个用于处理JavaScript的xml文件。
我有点工作但是收到的错误是图片的网址太长了。
这是我的代码 -
PHP
$sql = "SELECT image1 FROM $dbtable";
$result = mysqli_query($con, $sql);
echo '<?xml version="1.0" encoding="ISO-8859-1"?><Photos>';
while($row = mysqli_fetch_assoc($result)) {
$img = '"data:image/jpeg;base64,'.base64_encode($row['image1'] ).'"
height="100" width="100"';
echo "<photo>";
echo "<image>$img</image>";
echo "</photo>";
}
echo "</Photos>";
mysqli_free_result($result);
mysqli_close($con);
?>
的javascript
function getBookResponse() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var response = xhr.responseXML;
var theBooksDiv = document.getElementById("theBooksDiv");
theBooksDiv.innerHTML = "";
for (var i = 0; i < response.getElementsByTagName('photo').length; i++) {
var bookDiv = document.createElement("div");
bookDiv.classList.add("aBookDiv");
var imageValueString = response.getElementsByTagName('image')[i].firstChild.nodeValue;
var imageValueTag = createTagFromData("img");
imageValueTag.classList.add("bookImage");
imageValueTag.src = imageValueString;
theBooksDiv.appendChild(bookDiv);
bookDiv.appendChild(imageValueTag);
}
}
}
}
this is what is displayed in the browser
我感觉它与base64编码有关,所以有没有其他方法可以做到这一点?