如何将画布图像存储到mysql并使用nodejs检索它

时间:2019-03-27 05:55:51

标签: jquery html node.js

我有一个div,并使用canvas拍摄了该div的快照并将画布图像存储在变量getcanvas中,现在我想使用节点将该画布图像存储到mysqldatabase

1 个答案:

答案 0 :(得分:0)

两种方式

  • 将图像另存为dataUrl(即字符串)。
 /* send */
 const dataUrl = canvas.toDataURL()
 // TODO: post dataUrl to server and save it to mysql

 /* get */
 // get dataUrl from server
 const img = document.createElement('img');
 // attach dataUrl to img.src to show it
 img.src = dataUrl
  • 将画布图像保存为真实图像
/* send */
const dataUrl = canvas.toDataURL()
const [memeStr, dataStr] = (dataUrl || '').split(',')
const mime = memeStr
  .split(',')[0]
  .split(':')[1]
  .split(';')[0]

const bstr = atob(dataStr)
const u8arr = new Uint8Array(bstr.length)

for (let i = 0; i < bstr.length; i++) {
  u8arr[i] = bstr.charCodeAt(i)
}

const imageFile = new Blob([u8arr], { type: mime })
// TODO: you got the image file (by default is a png file)
// send it to the server and store its path in mysql

/* get */
// get imagePath from server
const img = document.createElement('img');
// attach imagePath to img.src to show it
img.src = imagePath