Home  >  Q&A  >  body text

ajax - How to verify that a blob file is an image in laravel?

If the form is submitted in the normal way, there will be no problem with background verification:

        $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()
            ]);

        }

However, when submitted using the formData object, the image file is converted into a blob file and cannot be verified:

        $('#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('提交失败,请尝试重新提交');
                });

            });

        });

How can the background verify that the blob file is an image?

仅有的幸福仅有的幸福2694 days ago740

reply all(3)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-16 16:51:55

    It’s very simple. Wouldn’t it be nice to make a temporary variable and restore it?
    $file = file_put_contents('/path/to/new/file_name', $blob);

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-16 16:51:55

    Extended validation rulesgetimagesize

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-16 16:51:55

    You can use base64 streaming

    reply
    0
  • Cancelreply