Ajax不是post大图的base64数据

Ajax not post base64 data of large image

我正在通过 ajax 将图像 base64 数据传递到我的 php 函数,但是对于大尺寸图像 Ajax 不会 post 数据。但是相同的代码非常适合小图像。

srcdata=data:image/jpeg;base64,/9j/4SxvRXhpZgAASUkqAAgAAAANAAABBAABAAAAwAwAAAEBBAABAAAALAcAAA8BAgAIAAAAqgAAABABAgARAAAAsgAAABIBAwABAAAABgAAABoBBQABAAAAxAAAABsBBQABAAAAzAAAACgBAwABAAAAAgAAADEBAgAMAAAA1AAAADIBAgAUAAAA4AAAABMCAwABAAAAAQAAAGmHBAABAAAA9AAAACWIBAABAAAA3gkAAPAJAABTQU1TVU5HAFNBTVNVTkctU0dILUk1MzcAAEgAAAABAAAASAAAAAEAAABJNTM3VUNVQ05DOQAyMDE0OjA2OjIyIDEwOjE3OjEwACEAmoIFAAEAAACGAgAAnYIFAAEAAACOAgAAIogDAAEAAAADAAAAJ4gDAAEAAAB9AAAAAJAHAAQAAAAwMjIwA5ACABQAAACWAgAABJACABQAAACqAgAAAZEHAAQAAAABAgMAAZIKAAEAAAC AgAAApIFAAEAAADGAgAAA5IKAAEAAADOAgAABJIKAAEAAADWAgAABZIFAAEAAADeAgAAB5IDAAEAAAACAAAACJIDAAEAAAAAAAAACZIDAAEAAAAAAAAACpIFAAEAAADmAgAAfJIHAGIAAABeCQAAhpIHAFwGAADuAgAAAKAHAAQAAAAwMTAwAaADAAEAAAABAAAAAqAEAAEAAADADAAAA6AEAAEAAAAsBwAABaAEAAEAAADACQAAF6IDAAEAAAACAAAAAaMDAAEAAAABAAAAAqQDAAEAAAAAAAAAA6QDAAEAAAAAAAAABKQFAAEAAABKCQAABqQDAAEAAAAAAAAACaQDAAEAAAAAAAAACqQDAAEAAAAAAAAAIKQCAAsAAABSCQAAAAAAAAEAAAAUAAAABAEAAGQAAAAyMDE0OjA2OjIyIDEwOjE3OjEwADIwMTQ6MDY6MjIgMTA6MTc6MTAAAQAAABQAAAAUAQAAZAAAAFAAAAABAA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UZBABg8AAAAPAAAAFMwOFFFTENHQzAxAAcAAQAHAAQAAAAwMTAwAgAEAAEAAAAAIAEADAAEAAEAAAAAAAAAEAAFAAEAAABaAAAAQAAEAAEAAAAAAAAAUAAEAAEAAAABAAAAAAEDAAEAAAAAAAAAAAAAAAAAAAAAAAAAAgABAAIABAAAAFI5OAACAAcABAAAADAxMDAAAAAAAQAAAAEABAAAAAICAAAAAAAACQAAAQQAAQAAAAABAAABAQQAAQAAAJAAAAADAQMAAQAAAAYAAAASAQMAAQAAAAYAAAAaAQUAAQA `

数据太大,所以部分 posted ,

ajax

count = 1;
name='test'
jQuery.ajax({
    url:"/createImage.php",
    data:{img_data:srcdata,img_id:count,name:name},
    type:"POST",
    contentType:"application/x-www-form-urlencoded",
    success:function(result)
    {
        alert(result);
    }
});

PHP

print_r($_POST); // is empty when logging..

当我试图在 php 中获取 post 数据时,我变得空了。 使用 base64 时 ajax 不发送数据的可能原因是什么。

我同意 TrueBlueAussie。这看起来像是 PHP.

中的服务器端设置

打开您的 php.ini 文件并找到说明 upload_max_filesize 的行。默认它设置为2M,即2MB。尝试将其增加到 3MB,看看您是否仍然收到错误消息。

注意:您需要重新启动 Apache/Nginx/webserver 才能使 php.ini 文件生效。

即使您将文件作为变量发送,application/x-www-form-urlencoded 也可能在 PHP 中被解释为实际文件上传。您可以查看的另一个 php.ini 设置是 post_max_size,默认值为 8M,这是您可以 POST 到 PHP 脚本的最大值。

首先:您在 base64 中指定的代码 returns 损坏的图像; 发送base64编码时,使用encodeURIComponent函数避免发送错误。

试试这个代码:

 var srcdata = "data:image/jpeg;base64,/9j/4SxvRXhpZgAASUkqAAgAAAANAAABBAABAAAAwAwAAAEBBAABAAAALAcAAA8BAgAIAAAAqgAAABABAgARAAAAsgAAABIBAwABAAAABgAAABoBBQABAAAAxAAAABsBBQABAAAAzAAAACgBAwABAAAAAgAAADEBAgAMAAAA1AAAADIBAgAUAAAA4AAAABMCAwABAAAAAQAAAGmHBAABAAAA9AAAACWIB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ST",
  url: "./createImage.php",
  data: { img_data:srcdata, img_id:count, name:name },
  cache: false,
  contentType: "application/x-www-form-urlencoded",
  success: function (result) {
   alert(result);
  }

 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>