duanhemou9834 2013-11-16 09:47
浏览 148
已采纳

too long

Hope someone help me with this mess. I am building a little mobile app that transforms a series of canvas objects to dataUrl then it encodes the data with btoa() method:

var canvas  = document.getElementById("imagen_envio");
var dataUrl = canvas.toDataURL('image/jpeg', .7);
var file = btoa(dataUrl);

After that it sends the file to a php file at the server using AJAX to store it in a Medium BLOB at MySQL. Everything it's working fine to this point but after that a php file must display the images but it's not working:

link: http://a.gob.mx/PHP_tratayretrata/img.php

//img.php
$connect = mysqli_connect($host, $user ,$password) or die("No se pudo conectar.");
mysqli_select_db($connect,$DB) or die("No se encuentra la base.");

$sql = "SELECT img FROM fotos WHERE usuario = 1 AND tema = 1 AND ejercicio = 1";
$result=mysqli_query($connect, $sql);

$row=mysqli_fetch_array($result);

header("Content-type: image/jpeg; charset=UTF-8");

$img = base64_decode($row["img"]);

echo '<img src="'.$img.'"/>';
?>

I made another php file that prints the data to text: http://a.gob.mx/PHP_tratayretrata/print-data.php and I used that data in a simple html file to be sure that the data is not corrupted: http://a.gob.mx/PHP_tratayretrata/test-data.html and it works!

So I have no idea what am I doing wrong. Any ideas? Thanks in advance!

  • 写回答

2条回答 默认 最新

  • doudi8231 2013-11-16 09:55
    关注

    You are base-64 encoding a string that is already base-64 encoded as toDataURL() returns a data-uri which contains a data-uri header before the actual base-64 data.

    This cannot be decoded into raw image-data.

    You need to chop off the header from your data-uri, send that as-is to server (base-64 string). You can store that in the data base and decode it later.

    /// this result is already a base-64 encoded string with a header
    var dataUrl = canvas.toDataURL('image/jpeg', .7);
    
    /// this will only base-64 encode again (incl. the header)
    //var file = btoa(dataUrl);
    

    A data-uri looks like this:

    data:[<MIME-type>][;charset=<encoding>][;base64],<data>
    

    So here you need to chop of the header up and including the last comma. The remaining data will be the base-64 encoded image.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?