简体   繁体   English

用ajax发布发送base64图像

[英]post sending base64 image with ajax

I'm trying to generate an image out of a canvas and send it to server through a POST request made with ajax. 我正在尝试从画布生成图像,并通过用ajax发出的POST请求将其发送到服务器。 I'm using html2canvas to convert a div to canvas and to convert it into base64 with toDataURL(). 我正在使用html2canvas将div转换为canvas并使用toDataURL()将其转换为base64。

here's my JS: 这是我的JS:

  function genImg(){ html2canvas(document.querySelector("#ogImage")).then(canvas => { var renderedImg = canvas.toDataURL() $.ajax({ type: "POST", url: "script.php", data: { base64Img: renderedImg } }).done(function(o){ console.log("saved") console.log(renderedImg); }) }); } 

And here's my script.php file: 这是我的script.php文件:

<?php
   $img = $_POST['data'];
   $img = str_replace('data:image/png;base64, ', '', $img);
   $img = str_replace(' ', '+', $img);
   $fileData = base64_decode($img);
   //saving
   $fileName = 'photo.png';
   file_put_contents($fileName, $fileData);
?>

Both scripts are working and a png is generated with the correct output name, but the generated png is empty. 这两个脚本都可以工作,并且使用正确的输出名称生成了png,但是生成的png为空。 I checked the received request and what is sent in post, both are different... console.log(renderedImg) is a really really long base64 code. 我检查了接收到的请求以及邮件发送的内容,两者是不同的... console.log(renderedImg)是一个非常长的base64代码。 and the request is also a base64 code but clearly shorter. 该请求也是base64代码,但明显较短。 Is there a limit of what can be send with POST request ? POST请求可以发送什么内容? or do I forgot something ? 还是我忘记了什么?

Also to check where my problem is, I copy/paste the javascript console.log(renderedImg) output and decoded it with https://www.base64decode.org/ the result is exactly the image i needed. 同样要检查我的问题所在,我复制/粘贴了javascript console.log(renderedImg)输出,并使用https://www.base64decode.org/对其进行解码,结果正是我需要的图像。 Then I tried with the POST request data and it's the same result as the generated png file, empty. 然后,我尝试使用POST请求数据,结果与生成的png文件相同,为空。

So I'm pretty sure that my problem is when the data are sent from ajax to PHP. 因此,我很确定我的问题是何时将数据从ajax发送到PHP。

Data url have the form data:[<media type>][;base64],<data> You have to strip the data:[<media type>][;base64], part before you decode. 数据url的格式为data:[<media type>][;base64],<data>您必须先剥离data:[<media type>][;base64],然后再解码。
Also use $_POST['base64Img'] to access the data as base64Img is what you used in your ajax request 还使用$_POST['base64Img']访问数据,因为base64Img是您在ajax请求中使用的内容

I finally managed to do it, The problem was located in the PHP file: 我终于设法做到了,问题出在PHP文件中:

<?php
 define('UPLOAD_DIR', 'images/');
 // previously it was $img = $_POST['data']
 $img = $_POST['imgBase64'];
 $img = str_replace('data:image/png;base64,', '', $img);
 $img = str_replace(' ', '+', $img);
 $data = base64_decode($img);
 $file = UPLOAD_DIR . 'test.png';
 $success = file_put_contents($file, $data);
 //send request to ocr
 print $success ? $file : 'Unable to save the file.';
?>

From now, my saved image is exactly the one needed 从现在开始,我保存的图像正是所需的图像

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

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