简体   繁体   English

以HTML表单提交Canvas数据

[英]Submit Canvas Data in an HTML Form

How can I submit Canvas content inside a PHP CodeIgniter form? 如何在PHP CodeIgniter表单中提交Canvas内容? One of the inputs on my form is a Canvas for a drawing or webcam photo. 我的表单上的一个输入是用于绘图或网络摄像头照片的Canvas

I created a tag <input name="picture" type="hidden"> and set its value to the base64 content: 我创建了一个标签<input name="picture" type="hidden">并将其值设置为base64内容:

myPictureInput.value = canvas.toDataURL();

However, when the user submits the form, the POST value read from the <input> is truncated to 64kb. 但是,当用户提交表单时,从<input>读取的POST值将被截断为64kb。

Is there an optimal way to post or submit images together with form data? 是否有最佳方式将图像与表单数据一起发布或提交?

As mentioned in this answer , you should send the canvas to your server separately, then put your form submit function inside of the done callback. 本回答所述 ,您应该将画布单独发送到服务器,然后将表单提交功能放在完成回调中。

var dataURL = canvas.toDataURL();
$(function() {
    $(form).submit(function(e) {
          e.preventDefault();
          $.ajax({
              type: "POST",
              url: "script.php",
              data: { 
                 imgBase64: dataURL
              }
         }).done(function(o) {
              console.log('saved image'); 
              $.post(url, form.serialize(), function(data) {
                     console.log('saved form')
              };
    });

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

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