浏览代码

Images: fileName experiments

Andrew Grant 2 月之前
父节点
当前提交
1d77945f18

+ 8 - 1
src/main/java/scot/carricksoftware/grants/controllers/images/images/ImageFormController.java

@@ -10,6 +10,9 @@ import org.springframework.ui.Model;
 import org.springframework.validation.BindingResult;
 import org.springframework.web.bind.annotation.ModelAttribute;
 import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RequestPart;
+import org.springframework.web.multipart.MultipartFile;
 import scot.carricksoftware.grants.commands.images.ImageCommand;
 import scot.carricksoftware.grants.constants.ImageMappingConstants;
 
@@ -17,6 +20,10 @@ import scot.carricksoftware.grants.constants.ImageMappingConstants;
 
 public interface ImageFormController {
 
+
+    @SuppressWarnings({"UnnecessaryLocalVariable", "unused"})
     @PostMapping(ImageMappingConstants.IMAGE)
-    String saveOrUpdate(@Valid @ModelAttribute ImageCommand imageCommand, BindingResult bindingResult, Model model);
+    String saveOrUpdate(@Valid @ModelAttribute ImageCommand imageCommand,
+                        @RequestParam MultipartFile file,
+                        BindingResult bindingResult, Model model);
 }

+ 7 - 2
src/main/java/scot/carricksoftware/grants/controllers/images/images/ImageFormControllerImpl.java

@@ -15,6 +15,8 @@ import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.ModelAttribute;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.multipart.MultipartFile;
 import scot.carricksoftware.grants.commands.images.ImageCommand;
 import scot.carricksoftware.grants.commands.images.ImageCommandImpl;
 import scot.carricksoftware.grants.constants.*;
@@ -67,11 +69,14 @@ public class ImageFormControllerImpl implements ImageFormController {
     @SuppressWarnings({"UnnecessaryLocalVariable", "unused"})
     @PostMapping(ImageMappingConstants.IMAGE)
     @Override
-    public String saveOrUpdate(@Valid @ModelAttribute ImageCommand imageCommand, BindingResult bindingResult, Model model) {
+    public String saveOrUpdate(@Valid @ModelAttribute ImageCommand imageCommand,
+                               @RequestParam MultipartFile file,
+                               BindingResult bindingResult, Model model) {
         logger.debug("ImageFormControllerImpl::saveOrUpdate");
 
         var z = imageCommand;
-
+        var x = file;
+        int debug = -1;
         imageCommandValidator.validate(imageCommand, bindingResult);
 
 

+ 4 - 9
src/main/resources/templates/images/image/form.html

@@ -23,7 +23,7 @@
 
 <div class="container border border-info rounded-3 text-center p-4">
     <h3>Image Details</h3>
-    <form th:object="${imageCommand}"  th:action="@{/image}" method="post" >
+    <form th:object="${imageCommand}"  th:action="@{/image}" method="post" enctype="multipart/form-data">
         <div th:if="${#fields.hasErrors('*')}" class="alert alert-danger">
             <p>Please Correct The Errors Below</p>
         </div>
@@ -65,17 +65,12 @@
                     </div>
                 </td>
             </tr>
-            <!--
             <tr>
-                <td style="text-align: right;">
-                    <label for="imageData">File :&nbsp;</label>
-                </td>
-                <td style="text-align: left;">
-                    <input type="file" name="imageData" id="imageData">
+                <td>File to upload:</td>
+                <td>
+                    <input type="file" name="file" />
                 </td>
             </tr>
-            --->
-
             <tr>
                 <td>&nbsp;</td>
             </tr>