创建了一个表,我无法从后端到数据库(Nodejs,react,MySQL)完全解析表的图像字段中的blob数据类型

问题描述 投票:0回答:1

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'))
mysql reactjs express
1个回答
0
投票
// 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" });
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.