[英]Saving canvas in MySQL database
我想将画布保存到MySQL数据库(BLOB单元)中。 我在javaScript中有功能
function saveCanvas(){
var canvas = document.getElementById("canvas");
var dataURL = canvas.toDataURL("image/png");
dataURL = dataURL.replace(/^data:image\/(png|jpeg);base64,/, "");
var req = $.ajax({
url: "Canvases",
type: "Post",
data: "operation=0&sessionId=" + readCookie('sessionId') + "&title=" + document.getElementById('imageNameTextbox').value + "&pic=" + dataURL,
success: function(){
}
});
}
然后我用Java存储我的图片
BASE64Decoder decoder = new BASE64Decoder();
byte[] pic = decoder.decodeBuffer(request.getParameter("pic"));
CanvasController.AddCanvas(sessionId, new CanvasModel(0, 0, request.getParameter("title"), pic));
并插入数据库:
String insertStatement = "INSERT INTO Canvas(userId, title, pic) VALUES (?, ?, ?);";
prepStmt = connection.prepareStatement(insertStatement);
prepStmt.setInt(1, id);
prepStmt.setString(2, canvas.getTitle());
prepStmt.setBytes(3, canvas.getPic());
prepStmt.executeUpdate();
运行后,我总是在db中得到空图像。 当我将image / png更改为image / jpeg并进行绘制时:
http://imageshack.us/photo/my-images/714/pobranec.jpg
我得到:
http://imageshack.us/photo/my-images/15/pobrane2q.jpg
你能告诉我我做错了吗?
您的数据字符串编码不正确,导致在服务器上接收到数据并从帖子正文中提取数据时导致数据损坏。 将您的data
更改为此:
data: {
operation: 0,
sessionId: readCookie('sessionId'),
title : document.getElementById('imageNameTextbox').value,
pic: dataURL
}
并让jquery为您处理编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.