简体   繁体   English

将画布保存在MySQL数据库中

[英]Saving canvas in MySQL database

I want to save my canvas into MySQL database (BLOB cell). 我想将画布保存到MySQL数据库(BLOB单元)中。 I have function in javaScript 我在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(){
        }
    });
}

Then I use Java to store my Image 然后我用Java存储我的图片

BASE64Decoder decoder = new BASE64Decoder();

 byte[] pic = decoder.decodeBuffer(request.getParameter("pic"));
 CanvasController.AddCanvas(sessionId, new CanvasModel(0, 0, request.getParameter("title"), pic));

And inserting into database: 并插入数据库:

  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();

After running that I always get empty image in db. 运行后,我总是在db中得到空图像。 When I change image/png to image/jpeg and draw for example: 当我将image / png更改为image / jpeg并进行绘制时:

http://imageshack.us/photo/my-images/714/pobranec.jpg http://imageshack.us/photo/my-images/714/pobranec.jpg

I get: 我得到:

http://imageshack.us/photo/my-images/15/pobrane2q.jpg http://imageshack.us/photo/my-images/15/pobrane2q.jpg

Can you tell what I'm doing wrong? 你能告诉我我做错了吗?

Your data string is not encoded properly, leading to corruption when the data's received on the server and extracted from the post body. 您的数据字符串编码不正确,导致在服务器上接收到数据并从帖子正文中提取数据时导致数据损坏。 Change your data to this: 将您的data更改为此:

    data: {
       operation: 0,
       sessionId:  readCookie('sessionId'),
       title : document.getElementById('imageNameTextbox').value,
       pic: dataURL
    }

and let jquery handle the encoding for you. 并让jquery为您处理编码。

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

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