简体   繁体   English

如何使用 react、fetchApi 和 nodejs 获取和插入图像到 Mysql

[英]How to fetch and insert image to Mysql using react,fetchApi and nodejs

I want to insert an image to mysql database receiving from < input type='file' / > and after fetching want to display it on < img /> using node and react我想将图像插入到从 < input type='file' /> 接收的 mysql 数据库中,并且在获取后想使用 node 在 < img /> 上显示它并做出反应

Below is my nodejs code .下面是我的 nodejs 代码。

    const {ISBN,publisher,category,edition,image,title,price,quantity,author,}=req.query
console.log(req.query)
    const insertIntoBooks=`INSERT into BOOKS(ISBN,publisher,category,edition,image,title,price,quantity,author) VALUES('${ISBN}','${publisher}','${category}','${edition}','${image}','${title}',${price},${quantity},'${author}')`
connection.execute(insertIntoBooks).then(resp=>res.json({data:'success'})).catch(err=>res.json({data:'error'}))
})

above u can see im receiving an image and this image is what i want to save in Mysql.上面你可以看到我正在接收一个图像,这个图像是我想保存在 Mysql 中的图像。 Below is my react code下面是我的反应代码



props.startInsertingBook(`http://localhost:3300/books/add?title=${title}&price=${price}&quantity=${quantity}&author=${authorName}&ISBN=${ISBN}&image=${image}&publisher=${publisher}&edition=${edition}&category=${category}`)

Below is my redux Action code.下面是我的 redux 操作代码。

return(dispatch)=>{
    return fetch(`${url}`).then(resp=>resp.json()).then(({data})=>{
        console.log(data);
        if(data==='success'){
          return  toastr.success('Success','Book added Successfuly')
        }else{
return toastr.error('Error','Error in submitting')
        }
    }).catch(err=>toastr.error('oops','error occur due to lost in coonectivity'))

}
}```

As image is in binary you can't send it directly through get method.由于图像是binary你不能直接通过 get 方法发送它。 You need to try fetch post method to achieve it.您需要尝试fetch post 方法来实现它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM