我从那里构建了一个映像裁剪工具,我想将该base64映像存储到服务器。我通过Ajax发送了它。图像以jpg格式存储,但问题是已损坏。任何人都可以建议我可以解决什么问题?通过ajax发送base64映像到服务器
这里是我的Ajax调用:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
method: 'post',
url: 'updateProfilePicture',
cache: false,
contentType: "application/json; charset=utf-8",
data: {'image': encodeURIComponent(profileImageUrl)},
success: function (data) {
alert(data);
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
这里是转换的base64正常图像控制器和存储到服务器:
public function updateProfile(Request $request)
{
$base64img = str_replace('data:image/jpeg;base64,', '', $request->Input(['image']));
$data = base64_decode($base64img);
$file = public_path() . '/users/' .'123123123.jpg';
file_put_contents($file, $data);
return \Response::json($data);
}
“损坏”是什么意思? – BenRoob
当我点击图片说'我们无法打开这个文件'。 – Hola
为什么在客户端和服务器中使用不同的内容类型? – jrook