Skip to content Skip to sidebar Skip to footer

Laravel And Angular Js File Upload

How I can save images with laravel and angular js ? are more inputs , but that work for me , are of type text My index:
Copy

<-- Angular Controller's File -->

$scope.uploadavtar = function(files) {
  var fd = new FormData();
  //Take the first selected file
  fd.append("file", files[0]);

  $http.post("/uploadavtar", fd, {
      withCredentials: true,
      headers: {'Content-Type': undefined },
      transformRequest: angular.identity
  }).then(functionsuccessCallback(response) {
    alert(response);
    // this callback will be called asynchronously// when the response is available
  }, functionerrorCallback(response) {
    alert(response);
    // called asynchronously if an error occurs// or server returns response with an error status.
  });
}

<-- In Route File -->

Route::post('/uploadavtar', 'UsersController@uploadavtar');

<-- In UsersController -->

publicfunctionuploadavtar(Request $request){
$user = JWTAuth::parseToken()->authenticate();
$user_id = $user->id;
$user_name = $user->first_name." ".$user->last_name;

$file = array('image' => Input::file('file'));
// setting up rules$rules = array('image' => 'required',); //mimes:jpeg,bmp,png and for max size max:10000// doing the validation, passing post data, rules and the messages$validator = Validator::make($file, $rules);
if ($validator->fails()) {
  // send back to the page with the input data and errorsreturn"validation failed";
}else {
    // checking file is valid.if (Input::file('file')->isValid()) {
      $destinationPath = 'assets/img'; // upload path$extension = Input::file('file')->getClientOriginalExtension(); // getting image extension$fileName = rand(11111,99999).'.'.$extension; // renameing image
      Input::file('file')->move($destinationPath, $user_name."_$user_id".".jpeg"); // uploading file to given path// sending back with messagereturn'Upload successfully';
    }
    else {
      // sending back with error message.return'uploaded file is not valid';
    }
}
}

Post a Comment for "Laravel And Angular Js File Upload"