英文:
Having trouble translating blob file into image on NextJS App from MySQL Database
问题
我正在处理一个项目,试图从数据库中提取一个存储为Blob文件的图像,但是图像无法显示。
第一个文件中的代码,products.jsx:
import React from "react";
import Image from 'next/image';
import { useEffect, useState } from "react";
import { StylesContext } from "@material-ui/styles";
export default function Home() {
const[dataResponse, setdataResponse] = useState([]);
useEffect(() => {
async function getPageData() {
const apiUrlEndpoint = 'http://localhost:3000/api/getdata';
const response = await fetch(apiUrlEndpoint);
const res = await response.json();
console.log(res.products);
setdataResponse(res.products)
}
getPageData();
}, []);
return (
<div className='bg-gray-100 min-h-screen'>
{dataResponse.map((products) => {
return (
<div key={products.ProductID}>
<div>{products.ProductName}</div>
<div>
<img src={`/${products.ProductImage}`} alt=""/>
</div>
<div>{products.ProductDescription}</div>
</div>
)
})}
</div>
);
}
来自api的代码,getdata.js:
import mysql from "mysql2/promise";
export default async function handler(req, res) {
const dbconnection = await mysql.createConnection({
host: "localhost",
database: "onlinestore",
port: 3306,
user: "root",
password: 不在此处显示,但密码是有效的。
});
try {
const query = "SELECT ProductID, ProductName, ProductDescription, ProductImage, ProductPrice, DateWhenAdded FROM products";
const values = [];
const[data] = await dbconnection.execute(query, values);
console.log(data);
dbconnection.end();
data.forEach((products) => {
products.ProductsImage = "data:image/webp;base64," + products.ProductImage;
});
res.status(200).json({ products: data });
} catch (error) {
res.status(500).json({ error: message });
}
}
有没有人知道如何让这个工作?如何让图像显示出来?
我尝试在getdata.js中添加一些内容以将其变成一个链接,但图像仍然不会显示。
英文:
So I'm working on a project and I'm trying to pull an image that's a blob file in my database. But the image is not coming up.
Code for the first file, products.jsx:
import React from "react";
import Image from 'next/image';
import { useEffect, useState } from "react";
import { StylesContext } from "@material-ui/styles";
export default function Home() {
const[dataResponse, setdataResponse] = useState([]);
useEffect(() => {
async function getPageData() {
const apiUrlEndpoint = 'http://localhost:3000/api/getdata';
const response = await fetch(apiUrlEndpoint);
const res = await response.json();
console.log(res.products);
setdataResponse(res.products)
}
getPageData();
}, []);
return (
<div className='bg-gray-100 min-h-screen'>
{dataResponse.map((products) => {
return (
<div key= {products.ProductID}>
<div>{products.ProductName}</div>
<div>
<img src={`/${products.ProductImage}`} alt=""/>
</div>
<div>{products.ProductDescription}</div>
</div>
)
})}
</div>
);
}
Code from api, getdata.js:
import mysql from "mysql2/promise";
export default async function handler(req, res) {
const dbconnection = await mysql.createConnection({
host: "localhost",
database: "onlinestore",
port: 3306,
user: "root",
password: Not showing this here, but the password works.
});
try {
const query = "SELECT ProductID, ProductName, ProductDescription, ProductImage, ProductPrice, DateWhenAdded FROM products"
const values = [];
const[data] = await dbconnection.execute(query, values);
console.log(data);
d
bconnection.end();
data.forEach((products) => {
products.ProductsImage = "data:image/webp;base64," + products.ProductImage;
}
);
res.status(200).json({ products: data });
} catch (error) {
res.status(500).json({ error: message });
}
Does anyone know how to get this to work? How to get the image to appear?
I tried to add in something to my getdata.js to make it a link, but the image still won't appear.
答案1
得分: 1
一般规则是,将图像存储在您的关系数据库管理系统 (RDBMS) 中并不是一个好主意。这会导致数据库膨胀,而文件系统是专门设计用于提供“文件”的数据库。此外,将图像以base64编码形式传递到JSON响应中,会导致传输数据大小增加约33%。有了这些说法,我会回答您的问题。
除非在存储在数据库中时图像已经以base64编码的形式存在,否则您似乎忘记了对其进行编码。
products.ProductsImage = "data:image/webp;base64," + products.ProductImage;
应该改为:
products.ProductImage = "data:image/webp;base64," + products.ProductImage.toString('base64');
以及
<img src={`/${products.ProductImage}`} alt="" />
可以简化为:
<img src={products.ProductImage} alt="" />
英文:
As a general rule, storing images in your RDBMS is not a good idea. It causes database bloat, and the file system is a database specifically designed for serving files
. Add to that, the base64 encoding of your images, to pass them in your JSON response, causes a ~33% increase in the size of the transmitted data. That said, I will answer your question.
Unless your image is already base64 encoded when stored in the database, you appear to have forgotten to encode it.
products.ProductsImage = "data:image/webp;base64," + products.ProductImage;
should be:
products.ProductImage = "data:image/webp;base64," + products.ProductImage.toString('base64');
And
<img src={`/${products.ProductImage}`} alt=""/>
can be just:
<img src={products.ProductImage} alt=""/>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论