我想将画布保存到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
你能说出我做错了吗?
答案 0 :(得分:1)
您的数据字符串编码不正确,导致在服务器上收到数据并从邮件正文中提取数据时出现损坏。将您的data
更改为此:
data: {
operation: 0,
sessionId: readCookie('sessionId'),
title : document.getElementById('imageNameTextbox').value,
pic: dataURL
}
让jquery为你处理编码。