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"));
}