【问题标题】:Saving canvas in MySQL database在 MySQL 数据库中保存画布
【发布时间】:2012-02-01 00:49:03
【问题描述】:

我想将我的画布保存到 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

你能告诉我我做错了什么吗?

【问题讨论】:

    标签: javascript mysql canvas


    【解决方案1】:

    您的数据字符串编码不正确,导致数据在服务器上接收并从帖子正文中提取时损坏。将您的 data 更改为:

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

    让 jquery 为你处理编码。

    【讨论】:

      猜你喜欢
      • 2016-07-21
      • 2018-05-23
      • 2016-08-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-22
      • 2012-05-28
      • 1970-01-01
      相关资源
      最近更新 更多