BAEL - 326 - Add section about file upload with additional form data (#1105)
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
22d26048b6
commit
a04c421305
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.model;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
public class FormDataWithFile {
|
||||
|
||||
private String name;
|
||||
private String email;
|
||||
private MultipartFile file;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public MultipartFile getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public void setFile(MultipartFile file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,11 +2,14 @@ package com.baeldung.web.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.baeldung.model.FormDataWithFile;
|
||||
|
||||
@Controller
|
||||
public class FileUploadController {
|
||||
|
||||
@@ -29,4 +32,21 @@ public class FileUploadController {
|
||||
modelMap.addAttribute("files", files);
|
||||
return "fileUploadView";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/uploadFileWithAddtionalData", method = RequestMethod.POST)
|
||||
public String submit(@RequestParam final MultipartFile file, @RequestParam final String name, @RequestParam final String email, final ModelMap modelMap) {
|
||||
|
||||
modelMap.addAttribute("name", name);
|
||||
modelMap.addAttribute("email", email);
|
||||
modelMap.addAttribute("file", file);
|
||||
return "fileUploadView";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/uploadFileModelAttribute", method = RequestMethod.POST)
|
||||
public String submit(@ModelAttribute final FormDataWithFile formDataWithFile, final ModelMap modelMap) {
|
||||
|
||||
modelMap.addAttribute("formDataWithFile", formDataWithFile);
|
||||
return "fileUploadView";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user