ajax异步上传

发布时间:2018-09-18 19:22:25 阅读:907次
  1 <!DOCTYPE html>
  2 <html>
  3 <head lang="en">
  4     <meta charset="UTF-8">
  5     <script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>
  6     <title></title>
  7 </head>
  8 <body>
  9 <form id="uploadForm" enctype="multipart/form-data">
 10     文件:<input id="file" type="file" name="file"/>
 11 </form>
 12 <button id="upload">上传文件</button>
 13 </body>
 14 <script type="text/javascript">
 15     $(function () {
 16         $("#upload").click(function () {
 17             var formData = new FormData($('#uploadForm')[0]);
 18             $.ajax({
 19                 type: 'post',
 20                 url: "http://test.api.feedback.com/feedback/upload",
 21                 data: formData,
 22                 cache: false,
 23                 processData: false,
 24                 contentType: false,
 25             }).success(function (data) {
 26                 alert(data);
 27             }).error(function () {
 28                 alert("上传失败");
 29             });
 30         });
 31     });
 32 </script>
 33 
 77     public function upload(Request $request, ApiResponse $response){
 78         $file = $request->file('file');
 79         
 80         $minetype = [ 'image/png', 'image/jpeg', 'image/jpg', 'image/gif' ];
 81             
 82         $fi = new \finfo(FILEINFO_MIME_TYPE);
 83         $ext = $fi->file($_FILES["file"]["tmp_name"]);
 84         if(!in_array($ext,$minetype)){
 85             return $response->failure('100001','文件格式不对!');
 86         }   
 87         
 88         $size = $file->getSize();
 89         if($size/1000>200){
 90             return $response->failure('100002','图片文件太大');
 91             exit;
 92         }   
 93         $path = $request->file('file')->store('public');
 94         //上传到cos源
 95         $path = $this->putFile2Cdn($path);
 96         return $response->success(['url'=>$path]);
 97     }                           

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询
上一篇:php 闭包

转载请注明:ajax异步上传 出自老鄢博客 | 欢迎分享