View layer
! ! ! Download jquery files and ajaxfileUpload plugin by yourself
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> </style> </head> <script type="text/javascript"> //The following is used for image upload preview function function setImagePreview(avalue) { var docObj=("doc"); var imgObjPreview=("preview"); if( &&[0]) { //Under Firefox, set the img attribute directly = 'block'; // = [0].getAsDataURL(); //The Firefox 7 or above cannot be obtained using the getAsDataURL() method above, and the following method is required = ([0]); } else { //Use filters under IE (); var imgSrc = ().text; var localImagId = ("localImag"); //The initial size must be set = "150px"; = "180px"; //Catch the image exception to prevent users from modifying the suffix to forge pictures try{ ="progid:(sizingMethod=scale)"; ("").src = imgSrc; } catch(e) { alert("The image you uploaded is incorrect, please re-select!"); return false; } = 'none'; (); } ajaxFileUpload(); //Upload the picture return true; } </script> <body> <center> <label> @foreach ($arr as $key=>$val) {{-- <img src="{{$val->n_img}}" alt="" height="100" width="100" class="qq"> <input type="file" style="VISIBILITY: hidden" >--}} <img width="100" height="100" src="{{$val->n_img}}"> <input type="file" name="touxiang" style="display:none" οnchange="javascript:setImagePreview();"> @endforeach </label> </center> </body> </html> <script src="./"></script> <script src="./"></script> <script type="text/javascript"> function ajaxFileUpload() { $.ajaxFileUpload ( { url: "{{url('up_img')}}", //The server-side request address for file upload secureuri: false, //Whether a security protocol is required, it is generally set to false fileElementId: 'doc' } ); return false; } </script>
Controller layer
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use DB; use Illuminate\Support\Facades\Input; class ImgController extends Controller { public function Index(){ $arr= DB::select("SELECT * FROM r_nav limit 1"); return view('index',['arr'=>$arr]); } //Modify the picture public function up_img(Request $Request){ $n_file = Input::file('touxiang'); if($n_file->isValid()){ //Get the file name $clientName = $n_file -> getClientOriginalName(); $realPath = $n_file -> getRealPath(); //Get picture format $entension = $n_file -> getClientOriginalExtension(); //Picture saving path $mimeTye = $n_file -> getMimeType(); $path = $n_file -> move('IMG'); } $ress = DB::table('r_nav')->where('n_id',11)->update(['n_img'=>$path]); } }
The above method to click on the user's avatar to change the avatar in laravel5.2 is all the content I share with you. I hope you can give you a reference and I hope you can support me more.