박민정

[update] Store image to server

...@@ -5,17 +5,18 @@ import axios from 'axios'; ...@@ -5,17 +5,18 @@ import axios from 'axios';
5 function ImageUpload() { 5 function ImageUpload() {
6 6
7 const imageDropEvent = (files) => { 7 const imageDropEvent = (files) => {
8 - let imageData = new imageData(); 8 + let imageData = new FormData();
9 9
10 const config = { 10 const config = {
11 header: {'content-type': 'multipart/image-data'} 11 header: {'content-type': 'multipart/image-data'}
12 } 12 }
13 + imageData.append("file", files[0])
13 14
14 // 이미지 전달 15 // 이미지 전달
15 axios.post('/api/product/image', imageData, config) 16 axios.post('/api/product/image', imageData, config)
16 .then(response => { 17 .then(response => {
17 if (response.data.success) { 18 if (response.data.success) {
18 - 19 + console.log(response.data)
19 } 20 }
20 else { 21 else {
21 alert('파일 저장을 실패했습니다.') 22 alert('파일 저장을 실패했습니다.')
......
...@@ -30,6 +30,7 @@ app.use(cors()) ...@@ -30,6 +30,7 @@ app.use(cors())
30 30
31 31
32 app.use('/api/users', require('./routes/users')); 32 app.use('/api/users', require('./routes/users'));
33 +app.use('/api/product', require('./routes/product'));
33 34
34 35
35 // 이미지 가져오려고 36 // 이미지 가져오려고
......
...@@ -2,6 +2,7 @@ const express = require('express'); ...@@ -2,6 +2,7 @@ const express = require('express');
2 const { User } = require("../models/User"); 2 const { User } = require("../models/User");
3 const { auth } = require("../middleware/auth"); 3 const { auth } = require("../middleware/auth");
4 const router = express.Router(); 4 const router = express.Router();
5 +const multer = require('multer');
5 6
6 7
7 var storage = multer.diskStorage({ 8 var storage = multer.diskStorage({
...@@ -13,12 +14,12 @@ var storage = multer.diskStorage({ ...@@ -13,12 +14,12 @@ var storage = multer.diskStorage({
13 } 14 }
14 }) 15 })
15 16
16 - var upload = multer({ storage: storage }) 17 +var upload = multer({ storage: storage }).single("file");
17 18
18 router.post('/image', (req, res) => { 19 router.post('/image', (req, res) => {
19 20
20 // 클라이언트로부터 받은 이미지 저장 21 // 클라이언트로부터 받은 이미지 저장
21 - upload(req, res, err => { 22 + upload(req, res, (err) => {
22 if (err) return req.json({ success: false, err }) 23 if (err) return req.json({ success: false, err })
23 return res.json({success: true, filePath: res.req.file.path, fileName: res.req.file.filename}) 24 return res.json({success: true, filePath: res.req.file.path, fileName: res.req.file.filename})
24 } 25 }
......