Laravel 5.5.How do I upload files with ajax?

could you tell me how to upload pictures asynchronously by laravel? I always operate in the wrong way!

js Code:

 onAjax(formObj,dataType)
    {
        var that = this;
        var form = $(formObj)[0];
        var data = this._formToData(form);//
        var url = form.action;
        var type = form.method;
        $.ajax({
            sync : false,
            url : url ,
            data : data ,
            type : type ,
            dataType : dataType ? dataType : "json",
            success(r)
            {
                console.log(r);
            },
            complete(r)
            {
                var response = r.responseJSON;
                var status = r.status;
                if(status === 200)
                {
                    return that.showNotifies(response.msg,"success");
                }
                that._getResponseError(response.errors);
                return that.showNotifies(response.message,"danger");
            }
        });
        return false;
    },

background code

public function edit($id,Request $request)
    {
        $this->_checkIsLoginUser($id);
        $user = User::find($id);

        $path = $request -> file("avatar") -> store("upload/avatar");
        $data = $request -> input();
        $data["avatar"] = $path;
        unset($data["_token"]);

        $this->validate($request, [
            "firstName" => "nullable|max:10",
            "lastName" => "nullable|max:10",
            "contact" => "nullable|integer",
            "birthday" => "nullable|date|max:10",
            "address" => "nullable|string|max:200",
            "city" => "nullable|string|max:50",
            "country" => "nullable|string|max:50",
            "postal" => "nullable|integer",
            "aboutMe" => "nullable|string|max:255",
            "avatar" => "image"
        ]);

        $user -> info = json_encode($data);
        $res = $user -> save();
        if($res)
        {
            return $this->_toAjax(__("notices.update.success"));
        }
        return $this->_toAjax(__("notices.update.danger"));
    }
Mar.01,2021

js Code:

< script type= "text/javascript" >

            $(function () {
                $("-sharpfile_upload").change(function () {
                    uploadImage();
                })
            })
            function uploadImage() { //  
                var imgPath = $("-sharpfile_upload").val();
                if (imgPath == "") {
                    alert("");
                    return;
                }
                //
                var strExtension = imgPath.substr(imgPath.lastIndexOf('.') + 1);
                if (strExtension != 'jpg' && strExtension != 'gif'
                    && strExtension != 'png' && strExtension != 'bmp') {
                    alert("");
                    return;
                }
                // var formData = new FormData($('-sharpart_form')[0]);
                var formData = new FormData();
                formData.append('fileupload',$('-sharpfile_upload')[0].files[0]);

                $.ajax({
                    type: "POST",
                    cache: false,
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    url: "/admin/upload",
                    data: formData,
                    contentType: false,
                    processData: false,
                    success: function(data) {
                        console.log(data);
                        $('-sharpart_thumb').attr('src', data);
                        $("input[name='map']").val(data);
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        alert("");
                    }
                });
            }
        </script>

backend code:

/ / File upload

public function upload(Request $request)
{
    $file = $request->file('fileupload');
 
   
    //
    if($file->isValid()) {
     //            
        $ext = $file->getClientOriginalExtension();    //
         //        
        $newfile = md5(date('YmdHis').rand(1000,9999).uniqid()).'.'.$ext;

         //       
        //
        $path = $file->move(public_path().'/uploads',$newfile);

        //        
        return '/uploads/'.$newfile;
    }
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e97b09-1678.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e97b09-1678.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?