如果是表单普通方式提交,后台验证没什么问题:
$file = $request->avatar;
$input = array('image' => $file);
$rules = array(
'image' => 'image'
);
$validator = \Validator::make($input, $rules);
if ( $validator->fails() ) {
return \Response::json([
'success' => false,
'errors' => $validator->getMessageBag()->toArray()
]);
}
可是,用formData对象提交,这个图片文件转换成了blob文件,就不能验证了:
$('#uploadAvatar').on('click', function (e) {
$('#uploadAvatar').html('正在保存...');
$("#image").cropper('getCroppedCanvas').toBlob(function (blob) {
var formData = new FormData();
formData.append('croppedImage', blob);
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: "POST",
url: "{{ url('/avatar') }}",
processData: false,
contentType: false,
cache: false,
data: formData
}).done(function (response) {
showResponse(response);
}).fail(function (data) {
alert('提交失败,请尝试重新提交');
});
});
});
后台怎么才可以验证blob文件是图片?
过去多啦不再A梦2017-05-16 16:51:55
很简单啊,搞个临时变量还原回来不就好了?$file = file_put_contents('/path/to/new/file_name', $blob);