박민정

[update] Store image to server

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