하루의 일상💜

[Spring] #7. 게시판 만들기_ 첨부파일 조회/수정/다운 본문

SpringBoot

[Spring] #7. 게시판 만들기_ 첨부파일 조회/수정/다운

도하루박 2022. 12. 29. 21:58
반응형
read.html
<div class="uploadResult">
    <ul>
        <th:block th:each="file:*{files}">
        <!-- th:object="${board}"에서 오브젝트를 받아온다. -->
            <li>
                <a th:href="@{/file/download(fileName=${file.fileUploadPath + '/' + file.fileUuid + '_' + file.fileName})}">
                    <img th:if="${file.fileImageCheck}" th:src="@{/file/display(fileName=${file.fileUploadPath} + '/s_' + ${file.fileUuid} + '_' + ${file.fileName})}">
                    <img th:unless="${file.fileImageCheck}" th:src="@{/images/attach.png}" width="100">
                </a>
                <p th:text="${file.fileName + '(' + file.fileSize / 1024 + 'KB)'}"></p>
            </li>
        </th:block>
    </ul>
</div>

수정페이지는 기존 upload와 동일하게 작성한다.

append의 Form name만 수정하기

<div class="content">
    <div class="form">
        <form method="post" th:action="@{/board/update}" th:object="${board}" id="updateForm">
            <div class="fields">
                <div class="field">
                    <h4>번호</h4>
                    <input type="text" th:field="*{boardNumber}" readonly/>
                </div>
                <div class="field">
                    <h4>*제목</h4>
                    <input type="text" th:field="*{boardTitle}"/>
                </div>
                <div class="field">
                    <h4>*내용</h4>
                    <textarea rows="6" style="resize:none" th:field="*{boardContent}"></textarea>
                </div>
                <div class="field">
                    <h4>작성자</h4>
                    <input type="text" th:field="*{boardWriter}" readonly/>
                </div>
                <div class="field">
                    <h4>첨부파일</h4>
                    <input type="file" name="upload" multiple>
                </div>
                <div class="field">
                    <div class="uploadResult">
                        <ul></ul>
                    </div>
                </div>
            </div>
            <ul class="actions special">
                <li>
                    <input type="submit" class="button" value="수정 완료"/>
                </li>
            </ul>
        </form>
    </div>
</div>


...


 $("form#updateForm").append(text).submit();

 

FileController  
//파일 다운로드   
   
   @GetMapping("/download")
    public ResponseEntity<Resource> download(String fileName) throws UnsupportedEncodingException {
        Resource resource = new FileSystemResource("C:/upload/" + fileName);
//        사용자에게 전달받은 파일의 full경로를 resorce에 전달을 한다.
        HttpHeaders header = new HttpHeaders();
        String name = resource.getFilename();
//        전달받은 resorce에서 해당 경로를 지우고 실제 파일이름만 가져온다. -> 다운할때에는 UUID가 삭제되고 원본 파일이름만 나와야 하기 때문에
        name = name.substring(name.indexOf("_") + 1);
        header.add("Content-Disposition", "attachment;filename=" + new String(name.getBytes(), "UTF-8"));
        return new ResponseEntity<>(resource, header, HttpStatus.OK);
    }

 

 

반응형