• 로그인
  • 장바구니에 상품이 없습니다.

home2 게시판 Spring 게시판 이미지 업로드 2 숙제

이미지 업로드 2 숙제

  • 이 주제에는 1개 답변, 2명 참여가 있으며 codingapple6 월 전에 전에 마지막으로 업데이트했습니다.
2 글 보임 - 1 에서 2 까지 (총 2 중에서)
  • 글쓴이
  • #134545

    이승호
    참가자
    write html 코드
    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <link href="/main.css" rel="stylesheet">
    </head>
    <body>
    <div th:replace="~{nav.html::navbar}"></div>
    <form action="/add" method="POST">
        <input name ="title">
        <input name ="price">
        <button type="submit">전송</button>
    </form>
    <input type="file" onchange="getURL(this)">
    < img src="">
    <script>
        async function getURL(e){
            let name = encodeURIComponent(e.files[0].name)
            let result = await fetch('/presigned-url?filename=' + name)
            result = await result.text()
            console.log(result)
            let uploadResult= await fetch(result, {
                method : 'PUT',
                body : e.files[0]
            })
            console.log(uploadResult)
            console.log(uploadResult.url.split("?")[0])
            if(uploadResult.ok){
                document.querySelector('img').src = uploadResult.url.split("?")[0]
                await fetch('/add', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({
                        fileUrl:uploadResult
                    })
                });
            }else {
                console.log("업로드 실패);
            }
        }
    </script>
    </body>
    </html>
    
    
    API
    @PostMapping("/add")
    String addPost(@RequestParam(name = "title") String title,
    @RequestParam(name = "price") Integer price,
    @RequestParam(name = "fileUrl") String imgUrl){
    itemService.saveItem(title, price, imgUrl);
    System.out.println(imgUrl);
    return "redirect:/list";
    }
    
    Item 코드
    @Entity
    @Data
    public class Item {
        @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        @Column(nullable = false)
        private String title;
        @Column(nullable = false)
        private Integer price;
        private String username;
        private String imageURL;
    
    
    이렇게 설정하고 했는데 안되는데 어떻게 해야하는지 잘모르겠습니다...
    
    #134556

    codingapple
    키 마스터
    어떻게 안됩니까 크롬콘솔에 에러메세지는 안뜹니까
2 글 보임 - 1 에서 2 까지 (총 2 중에서)
  • 답변은 로그인 후 가능합니다.

About

현재 월 700명 신규수강중입니다.

  (09:00~20:00) 빠른 상담은 카톡 플러스친구 코딩애플 (링크)
  admin@codingapple.com
  이용약관
ⓒ Codingapple, 강의 예제, 영상 복제 금지
top

© Codingapple, All rights reserved. 슈퍼로켓 에듀케이션 / 서울특별시 강동구 고덕로 19길 30 / 사업자등록번호 : 212-26-14752 온라인 교육학원업 / 통신판매업신고번호 : 제 2017-서울강동-0002 호 / 개인정보관리자 : 박종흠