Ajax不发布大图像的base64数据

时间:2015-02-03 10:10:23

标签: javascript php jquery ajax base64

我将图像base64数据通过ajax传递给我的php函数,但对于大尺寸图像,Ajax不会发布数据。但是相同的代码适用于小图像。

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

数据太大而部分发布,

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中获取发布数据时,我变空了。 使用base64时,ajax不发送数据的原因可能是什么。

2 个答案:

答案 0 :(得分:5)

我同意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脚本的最大值。

答案 1 :(得分:1)

首先:你在base64中指出的代码返回一个损坏的图像; 当您在base64中发送代码时,使用函数encodeURIComponent来避免发送时出错。

试试这段代码:



	var srcdata = "data:image/jpeg;base64,/9j/4SxvRXhpZgAASUkqAAgAAAANAAABBAABAAAAwAwAAAEBBAABAAAALAcAAA8BAgAIAAAAqgAAABABAgARAAAAsgAAABIBAwABAAAABgAAABoBBQABAAAAxAAAABsBBQABAAAAzAAAACgBAwABAAAAAgAAADEBAgAMAAAA1AAAADIBAgAUAAAA4AAAABMCAwABAAAAAQAAAGmHBAABAAAA9A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
		type: "POST",
		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>
&#13;
&#13;
&#13;