Submit Canvas Data in an HTML Form

笑着哭i 提交于 2021-01-27 12:16:26

问题


How can I submit Canvas content inside a PHP CodeIgniter form? One of the inputs on my form is a Canvas for a drawing or webcam photo.

I created a tag <input name="picture" type="hidden"> and set its value to the base64 content:

myPictureInput.value = canvas.toDataURL();

However, when the user submits the form, the POST value read from the <input> is truncated to 64kb.

Is there an optimal way to post or submit images together with form data?


回答1:


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')
              };
    });


来源:https://stackoverflow.com/questions/34048093/submit-canvas-data-in-an-html-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!