<form id= "uploadForm"> <p >上传文件: <input type="file" name="file"/></p> <input type="button" value="上传" οnclick="doUpload()" /> </form> <script src="./js/jq.js"></script> <script> function doUpload() { var formData = new FormData($( "#uploadForm" )[0]); var url = 'http://localhost/demo/laravel/public/exam2_file'; formData.append("url",url); $.ajax({ url: 'exam2_file' , type: 'POST', data: formData, async: false, cache: false, contentType: false, dataType:'json', processData: false, success: function (returndata) { // alert(returndata.data); $("#aaa").attr('src','./uploads/'+returndata.data); }, error: function (returndata) { // alert(returndata.data); $("#aaa").attr('src','./uploads/'+returndata.data); } }); } </script> <img src="" alt="" id="aaa"> {{--{!! Form::open(['url' => 'foo/bar']) !!}--}} {{--//--}} {{--{!! Form::close() !!}--}}public function exam2_file() { $n_file = Input::file("file"); $url = Input::get(); if($n_file->isValid()){ //获取文件名称 $clientName = $n_file -> getClientOriginalName(); $realPath = $n_file -> getRealPath(); //获取图片格式 $entension = $n_file -> getClientOriginalExtension(); //图片保存路径 $mimeTye = $n_file -> getMimeType(); $newName = base64_encode(rand(10000000,999999999)).'.'.$entension; $path = $n_file -> move('./uploads',$newName); if($path){ $data['code']='0'; $data['msg']='ok'; $data['data']=$newName; echo json_encode($data); }else{ $data['code']='1'; $data['msg']='fail'; echo json_encode($data); } } }
laravel上传图片调用接口,使用formData 上传图片 展示在本页面
最新推荐文章于 2023-04-07 10:03:56 发布