AE86 4 lat temu
rodzic
commit
1664525ad1

+ 17 - 1
dbsyncer-web/src/main/java/org/dbsyncer/web/config/AppConfig.java

@@ -2,10 +2,14 @@ package org.dbsyncer.web.config;
 
 import org.dbsyncer.web.remote.UserService;
 import org.dbsyncer.web.remote.UserServiceImpl;
+import org.springframework.boot.web.servlet.MultipartConfigFactory;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;
 import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
+import org.springframework.util.unit.DataSize;
+
+import javax.servlet.MultipartConfigElement;
 
 /**
  *
@@ -41,4 +45,16 @@ public class AppConfig {
         return proxy;
     }
 
-}
+    @Bean
+    public MultipartConfigElement multipartConfigElement() {
+        MultipartConfigFactory factory = new MultipartConfigFactory();
+        //上传的单个文件最大值   KB,MB 这里设置为10MB
+        DataSize maxSize = DataSize.ofMegabytes(10);
+        DataSize requestMaxSize = DataSize.ofMegabytes(30);
+        factory.setMaxFileSize(maxSize);
+        /// 设置一次上传文件的总大小
+        factory.setMaxRequestSize(requestMaxSize);
+        return factory.createMultipartConfig();
+    }
+
+}

+ 3 - 4
dbsyncer-web/src/main/java/org/dbsyncer/web/controller/upload/UploadController.java

@@ -32,14 +32,13 @@ public class UploadController {
 
     @RequestMapping(value = "/upload")
     @ResponseBody
-    public RestResult upload(HttpServletRequest request, @RequestParam("file") MultipartFile[] files) {
+    public RestResult upload(MultipartFile[] files) {
         try {
-            if (files != null) {
-                int length = files.length;
+            if (files != null && files.length > 0) {
                 MultipartFile file = null;
                 String filePath = pluginService.getPluginPath();
                 FileUtils.forceMkdir(new File(filePath));
-                for (int i = 0; i < length; i++) {
+                for (int i = 0; i < files.length; i++) {
                     file = files[i];
                     if (file != null) {
                         File dest = new File(filePath + file.getOriginalFilename());

+ 31 - 20
dbsyncer-web/src/main/resources/public/upload/upload.html

@@ -3,48 +3,59 @@
       lang="zh-CN">
 
 <div class="container">
-    <form class="form-horizontal" role="form">
+    <form id="uploadForm" class="form-horizontal" role="form">
         <div class="row text-center">
-            <h3>文件上传测试</h3>
+            <h3>上传插件</h3>
         </div>
 
-        <!-- 操作 -->
-        <div class="row">
-            <form method="post" enctype="multipart/form-data">
-                File:<input id="formFile" type="file" name="flie"/>
-                <br>
-                <input id="subBtn" type="submit" value="Submit"/>
-            </form>
+        <div class="form-group">
+            <label class="col-sm-2 control-label">插件 <strong class="driverVerifcateRequired">*</strong></label>
+            <div class="col-sm-10">
+                <input id="formFile" type="file" accept="application/zip" name="file" dbsyncer-valid="require" multiple="multiple" />
+            </div>
+        </div>
+
+        <div class="form-group">
+            <label class="col-sm-2 control-label"></label>
+            <div class="col-sm-10">
+                <button id="subBtn" type="button" class="btn btn-default">
+                    <span class="fa fa-arrow-circle-o-up"></span>上传
+                </button>
+            </div>
         </div>
     </form>
 </div>
 
 <script type="text/javascript">
     $("#subBtn").click(function(){
-        // 创建表单数据对象
-        var obj = new FormData();
+        var $form = $("#uploadForm");
+        if (!$form.formValidate()) {
+            return;
+        }
+
+        var data = new FormData();
         // 获取文件框中的数据
-        var file = document.getElementById("formFile").files[0];
+        var files = document.getElementById("formFile").files[0];
+        console.log(files);
         // 将文件数据添加至表单数据对象中
-        obj.append("file", file);
+        data.append("files", files);
         $.ajax({
             url:'/upload/upload',
             type:'POST',
-            data: obj,
+            data: data,
             processData : false, // 使数据不做处理
             contentType : false, // 不要设置Content-Type请求头
             mimeType : 'multipart/form-data',
+            dataType: 'json',
             success: function(data){
                 console.log(data);
-                if (data.status == 'ok') {
-                    alert('上传成功!');
+                if (data.success == true) {
+                    bootGrowl(data.resultValue, "success");
+                } else {
+                    bootGrowl(data.resultValue, "danger");
                 }
-            },
-            error:function(response){
-                console.log(response);
             }
         });
     })
-
 </script>
 </html>