回答編集履歴

2

微調整

2021/07/11 09:04

投稿

IT001
IT001

スコア618

test CHANGED
@@ -200,14 +200,22 @@
200
200
 
201
201
  public String confirm(@RequestParam("filedata") MultipartFile filedata, Model model) throws IOException {
202
202
 
203
+ if (filedata.getContentType().startsWith("image/")) {
204
+
203
- model.addAttribute("contentType", filedata.getContentType());
205
+ model.addAttribute("contentType", filedata.getContentType());
206
+
207
+ } else {
208
+
209
+ model.addAttribute("keiyakuMaster", new KeiyakuMaster());
210
+
211
+ return "index";
212
+
213
+ }
204
214
 
205
215
  model.addAttribute("filename", filedata.getOriginalFilename());
206
216
 
207
217
  model.addAttribute("viewFiledata", Base64.getEncoder().encodeToString(filedata.getBytes()));
208
218
 
209
-
210
-
211
219
  return "index";
212
220
 
213
221
  }
@@ -268,4 +276,6 @@
268
276
 
269
277
 
270
278
 
279
+
280
+
271
- ```
281
+ ```

1

確認画面の追加

2021/07/11 09:04

投稿

IT001
IT001

スコア618

test CHANGED
@@ -72,20 +72,48 @@
72
72
 
73
73
  <body>
74
74
 
75
+ <th:block th:if="${#strings.isEmpty(filename)}">
76
+
75
- <form th:action="@{/}" th:object="${keiyakuMaster}" method="post" enctype="multipart/form-data">
77
+ <form th:action="@{/}" th:object="${keiyakuMaster}" method="post" enctype="multipart/form-data">
76
-
78
+
77
- <input type="file" th:field="*{filedata}" accept="image/png, image/jpeg"><br>
79
+ <input type="file" th:field="*{filedata}" accept="image/png, image/jpeg"><br>
78
-
80
+
79
- <br>
81
+ <br>
80
-
82
+
81
- <button>画像アップロード</button>
83
+ <button>アップロードの確認へ</button>
82
-
84
+
83
- </form>
85
+ </form>
86
+
87
+ </th:block>
88
+
89
+ <th:block th:unless="${#strings.isEmpty(filename)}">
90
+
91
+ <p>以下の画像を、アップロードしてもよろしいですか?</p>
92
+
93
+ <form th:action="@{/save}" method="post" enctype="multipart/form-data">
94
+
95
+ <button>画像のアップロード</button><br>
96
+
97
+ <br>
98
+
99
+ <input type="hidden" name="contentType" th:value="${contentType}">
100
+
101
+ <input type="hidden" name="filename" th:value="${filename}">
102
+
103
+ <input type="hidden" name="viewFiledata" th:value="${viewFiledata}">
104
+
105
+ <img th:src="@{'data:' + ${contentType} + ';base64,'+${viewFiledata}}" alt="">
106
+
107
+ </form>
108
+
109
+ </th:block>
84
110
 
85
111
  </body>
86
112
 
87
113
  </html>
88
114
 
115
+
116
+
89
117
  ```
90
118
 
91
119
 
@@ -112,7 +140,7 @@
112
140
 
113
141
  <body>
114
142
 
115
- <p>以下の画像が、アップロードされてい</p>
143
+ <p style="color:red">以下の画像が、アップロードされました</p>
116
144
 
117
145
  <img th:if="!${#strings.isEmpty(filedata)}" th:src="@{'data:' + ${contentType} + ';base64,'+${filedata}}" alt="">
118
146
 
@@ -170,13 +198,39 @@
170
198
 
171
199
  @PostMapping("/")
172
200
 
173
- public String save(@RequestParam("filedata") MultipartFile filedata) throws IOException {
201
+ public String confirm(@RequestParam("filedata") MultipartFile filedata, Model model) throws IOException {
202
+
203
+ model.addAttribute("contentType", filedata.getContentType());
204
+
205
+ model.addAttribute("filename", filedata.getOriginalFilename());
206
+
207
+ model.addAttribute("viewFiledata", Base64.getEncoder().encodeToString(filedata.getBytes()));
208
+
209
+
210
+
211
+ return "index";
212
+
213
+ }
214
+
215
+
216
+
217
+ @PostMapping("/save")
218
+
219
+ public String save(
220
+
221
+ @RequestParam("contentType") String contentType,
222
+
223
+ @RequestParam("filename") String filename,
224
+
225
+ @RequestParam("viewFiledata") String viewFiledata) throws IOException {
226
+
227
+
174
228
 
175
229
  var keiyakuMaster = new KeiyakuMaster();
176
230
 
177
- if (filedata.getContentType().startsWith("image/")) {
231
+ if (contentType.startsWith("image/")) {
178
-
232
+
179
- keiyakuMaster.setContentType(filedata.getContentType());
233
+ keiyakuMaster.setContentType(contentType);
180
234
 
181
235
  } else {
182
236
 
@@ -184,16 +238,12 @@
184
238
 
185
239
  }
186
240
 
187
- keiyakuMaster.setFilename(filedata.getOriginalFilename());
188
-
189
- keiyakuMaster.setFiledata(filedata.getBytes());
241
+ keiyakuMaster.setFilename(filename);
242
+
190
-
243
+ keiyakuMaster.setFiledata(Base64.getDecoder().decode(viewFiledata));
191
-
192
244
 
193
245
  keiyakuMasterRepository.save(keiyakuMaster);
194
246
 
195
-
196
-
197
247
  return "redirect:/" + keiyakuMaster.getId();
198
248
 
199
249
  }
@@ -206,20 +256,16 @@
206
256
 
207
257
  var keiyakuMaster = keiyakuMasterRepository.findById(id).get();
208
258
 
209
-
210
-
211
259
  model.addAttribute("contentType", keiyakuMaster.getContentType());
212
260
 
213
- model.addAttribute("filename", keiyakuMaster.getFilename());
214
-
215
261
  model.addAttribute("filedata", Base64.getEncoder().encodeToString(keiyakuMaster.getFiledata()));
216
262
 
217
-
218
-
219
263
  return "result";
220
264
 
221
265
  }
222
266
 
223
267
  }
224
268
 
269
+
270
+
225
- ```
271
+ ```