简体   繁体   English

将 javascript 自动生成的文件附加到 POST 表单并在 $_FILES 中的 PHP 上处理它

[英]Append javascript auto-generated file to POST form and process it on PHP in $_FILES

I'm trying to append file to a form and send it via POST request.我正在尝试将文件附加到表单并通过 POST 请求发送它。

The problem is the file generates in js script (it's cropped image in base64) Next, I convert it from base64 to blob.问题是在js脚本中生成的文件(它在base64中裁剪图像)接下来,我将它从base64转换为blob。 And there no idea how to link this blob to my form.并且不知道如何将这个 blob 链接到我的表单。

I can do it via ajax , but looking for way to send image as file without xhr/ajax and proccess it of PHP side in $_FILES .我可以通过ajax 来完成,但正在寻找将图像作为文件发送而不使用 xhr/ajax 并在$_FILES中处理 PHP 端的方法。

Don't even know is it possible甚至不知道有没有可能

My form has attr 'multipart/form-data'我的表单有attr 'multipart/form-data'

you can create a hidden canvas and in that canvas you can draw the image with your base64 encoded string.您可以创建一个隐藏的画布,并在该画布中使用 base64 编码的字符串绘制图像。 Something like below像下面这样

<canvas style="height:200px;width:200px;display:none;" id="picture"></canvas>
<input type="hidden" name="clicked_image" id="clicked_image" />

and then the javascript code as然后是javascript代码

<script type="text/javascript">
var videoPicture = 'your base64 string';
var hiddenPictureField = document.querySelector('input#clicked_image');
var canvasPicture = window.canvas = document.querySelector('canvas#picture');
canvasPicture.width = 480;
canvasPicture.height = 360;
canvasPicture.getContext('2d').
drawImage(videoPicture, 0, 0, canvasPicture.width, canvasPicture.height);
hiddenPictureField.value = canvasPicture.toDataURL("image/png");
</script>

And then you will get the image in the clicked_image field.然后您将在 clicked_image 字段中获得图像。 The base 64 string you can process it accordingly.您可以相应地处理 base 64 字符串。

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

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