使用 Ajax 和 PHP $_FILES 从 Canvas 元素发送图像

Sending images from Canvas elements using Ajax and PHP $_FILES(使用 Ajax 和 PHP $_FILES 从 Canvas 元素发送图像)
本文介绍了使用 Ajax 和 PHP $_FILES 从 Canvas 元素发送图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我需要能够将图像和一些表单字段从客户端画布元素发送到 PHP 脚本,以 $_POST 和 $_FILES 结尾.当我这样发送时:

I need to be able to send an image and some form fields from a client side canvas element to a PHP script, ending up in $_POST and $_FILES. When I send it like this:

<script type="text/javascript">
var img = canvas.toDataURL("image/png");
...
ajax.setRequestHeader('Content-Type', "multipart/form-data; boundary=" + boundary_str);
var request_body = boundary + '
' 
+ 'Content-Disposition: form-data; name="formfield"' + '
' 
+ '
' 
+ formfield + '
' 
+ '
' 
+ boundary + '
'
+ 'Content-Disposition: form-data; name="async-upload"; filename="' 
+ "ajax_test64_2.png" + '"' + '
' 
+ 'Content-Type: image/png' + '
' 
+ '
' 
+ img
+ '
' 
+ boundary;
ajax.send(request_body);
</script>

$_POST 和 $_FILES 都返回填充,但 $_FILES 中的图像数据仍然需要像这样解码:

$_POST and $_FILES both come back populated, but the image data in $_FILES still needs decoding like this:

$loc = $_FILES['async-upload']['tmp_name'];
$file = fopen($loc, 'rb');
$contents = fread($file, filesize($loc));
fclose($file);
$filteredData=substr($contents, strpos($contents, ",")+1);
$unencodedData=base64_decode($filteredData);

...为了将其保存为可读的 PNG.这不是一个选项,因为我正在尝试将图像传递给 Wordpress 的 media_handle_upload() 函数,该函数需要指向可读图像的 $_FILES 索引.我也无法相应地解码、保存和更改tmp_name",因为它违反了安全检查.

...in order to save it as a readable PNG. This isn't an option as I'm trying to pass the image to Wordpress's media_handle_upload() function, which requires an index to $_FILES pointing to a readable image. I also can't decode, save and alter 'tmp_name' accordingly, as it falls foul of security checks.

所以,我发现了这个:http://www.webtoolkit.info/javascript-base64.html并尝试在客户端进行解码:

So, I found this: http://www.webtoolkit.info/javascript-base64.html and tried to do the decode on the client side:

img_split = img.split(",",2)[1];
img_decoded = Base64.decode( img_split );

但由于某种原因,当它到达 PHP 时,我仍然没有得到一个可读的文件.所以问题是:为什么?"或我做错了什么?"或者这可能吗?":-)

but for some reason I still don't end up with a readable file when it gets to the PHP. So the question is: "Why?" or "What am I doing wrong?" or "Is this even possible?" :-)

非常感谢任何帮助!

谢谢,凯恩

推荐答案

以 Nathan 的出色回答为基础,我能够完善它,以便它仍然通过 jQuery.ajax.只需将其添加到 ajax 请求中:

Building on Nathan's excellent answer, I was able to finnagle it so that it is still going through jQuery.ajax. Just add this to the ajax request:

            xhr: function () {
                var myXHR = new XMLHttpRequest();
                if (myXHR.sendAsBinary == undefined) {
                    myXHR.legacySend = myXHR.send;
                    myXHR.sendAsBinary = function (string) {
                        var bytes = Array.prototype.map.call(string, function (c) {
                            return c.charCodeAt(0) & 0xff;
                        });
                        this.legacySend(new Uint8Array(bytes).buffer);
                    };
                }
                myXHR.send = myXHR.sendAsBinary;
                return myXHR;
            },

基本上,您只需返回一个被覆盖的 xhr 对象,以便send"表示sendAsBinary".然后 jQuery 做正确的事.

Basically, you just return back an xhr object that is overriden so that "send" means "sendAsBinary". Then jQuery does the right thing.

这篇关于使用 Ajax 和 PHP $_FILES 从 Canvas 元素发送图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)