I’m working on a web app where I need to fetch image data stored as BLOB in my database and display it on the webpage. My Flask backend retrieves the image correctly, but I’m having trouble showing it in the frontend.
<!DOCTYPE html>
<html>
<head>
<title>Image Viewer</title>
</head>
<body>
<h2>Fetch Image</h2>
<form id="imageForm">
<label>Image ID:</label>
<input type="text" id="img_id" name="img_id">
<br><br>
<button type="submit">Load Image</button>
</form>
<div id="result">
<img id="display_img" src="" alt="Loaded image">
</div>
<script>
document.getElementById('imageForm').addEventListener('submit', function(e) {
e.preventDefault();
var imageId = document.getElementById('img_id').value;
fetch('/loadImage', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({img_id: imageId})
})
.then(response => response.blob())
.then(blob => {
var imageUrl = URL.createObjectURL(blob);
document.getElementById('display_img').src = imageUrl;
});
});
</script>
</body>
</html>
My Flask route returns the image like this:
return Response(blob_data, mimetype='image/jpeg')
The image shows up in browser dev tools but not on the actual page. What am I missing here?