Createlaptop.Jsx 文件是前端,它有一个表单,将所有值(名称、gen、sir、hasGraphicCard、价格、图像)传递到 server.Jsx 后端,其中这些查询在表中创建新字段。唯一的问题是,当我在图像文件文本框中输入这个值“LOAD_FILE('F:\React_Internships\of1.jpeg')”时,它会在数据库表中创建 44butes 的 blob 数据,而如果执行 sql 命令,它会创建 64kb 的数据
这个MySQL命令无法完全执行从后端到我的MySQL的值
加载图像后仅传递 44Bytes 而不是整个 64KB。这是 MySQL 查询
CreateLaptop.jsx---frontend
const handleImageChange = (e) => {
const filePath = e.target.value;
console.log(filePath);
setBook((prev) => ({ ...prev, image: filePath }));
};
return (
<div class="form">
<input
type="text" // Change input type to text
name="image"
placeholder="File path"
onChange={handleImageChange}
/>
</div>
);
server.jsx----backend
app.post("/books", (req, res) => {
try {
const { name, gen, sir, hasGraphicCard, price ,image} = req.body;
const q = "INSERT INTO laps(`name`, `gen`, `sir`, `hasGraphicCard`, `price`, `image`) VALUES
(?, ?, ?, ?, ?,?)";
const values = [name, gen, sir, hasGraphicCard, price, image];
db.query(q, values, (err, data) => {
if (err) {
console.error("Error inserting book data:", err);
res.status(500).json({ error: "Internal server error" });
return;
}
res.json({ message: "laptop data inserted successfully", data });
});
} catch (error) {
console.error("Error handling file upload:", error);
res.status(500).json({ error: "Internal server error" });
}
});
database-------query
INSERT INTO laps (name, gen, sir, hasGraphicCard, price, image)
VALUES ('Laptop Name', 'Laptop Generation', 'Laptop Serial number', true, 999.99,
LOAD_FILE('F:\\of1.jpeg'))
// Frontend: CreateLaptop.jsx
const handleImageChange = (e) => {
const filePath = e.target.value.replace(/\\/g, '\\\\'); // Replace single backslashes with double backslashes
console.log(filePath);
setBook((prev) => ({ ...prev, image: filePath }));
};
// Backend: server.jsx
app.post("/books", (req, res) => {
try {
const { name, gen, sir, hasGraphicCard, price, image } = req.body;
const q = "INSERT INTO laps(`name`, `gen`, `sir`, `hasGraphicCard`, `price`, `image`) VALUES (?, ?, ?, ?, ?, ?)";
const values = [name, gen, sir, hasGraphicCard, price, `LOAD_FILE('${image}')`]; // Inserting image path directly into the query
db.query(q, values, (err, data) => {
if (err) {
console.error("Error inserting book data:", err);
res.status(500).json({ error: "Internal server error" });
return;
}
res.json({ message: "laptop data inserted successfully", data });
});
} catch (error) {
console.error("Error handling file upload:", error);
res.status(500).json({ error: "Internal server error" });
}
});