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 }