I want to receive the blob image stored in the longblob type 'menuImg' column and put it in the img tag with the id 'img'. Help me!
['/admin/getMenuImg' Router source]
router.get('/getMenuImg', function (req, res) {
//var menuNo = req.body.menuNo;
var query = `SELECT menuImg FROM menu WHERE menuNo = 1`;
connection.query(query, function (error, result) {
if(error) {
console.log("error ocurred: ", error);
res.json({ "code": 400, "result": "error ocurred" })
} else {
console.log("get menuImg success");
res.json({"code": 200, "result": "get menuImg success", "menuImg": result[0].menuImg})
}
})
})
[html source]
<body>
<img id="img" width="400" height="400">
</body>
<script>
$.ajax({
url:'/admin/getMenuImg', // Maybe this is wrong
cache:false,
xhr:function(){
var xhr = new XMLHttpRequest();
xhr.responseType= 'blob'
return xhr;
},
success: function(data){
var img = document.getElementById('img');
var url = window.URL || window.webkitURL;
img.src = url.createObjectURL(data.menuImg);
},
error:function(){
}
});
</script>