티스토리 뷰
반응형
React에서 FormData()를 사용하여 File 및 Json,Array 데이터 넘기기에 이어 Server단 코드 입니다.
https://seung-min.tistory.com/44
[React] FormData()사용 file 및 json, array 데이터 보내기
위 사진과 같이 파일과 파일에 해당하는 파일명, 설명, 태그 등 사용자에게 입력 받은 값을 서버로 전송해야할 일이 있습니다. file 만 보내는글은 많지만 json까지 같이 보내는건 많지 않아서 작
seung-min.tistory.com
넘어온 데이터를 보면 다음과 같습니다.
위의 결과에서 보면 Json,array,File이 한번에 넘어온걸 볼 수 있습니다.
const multer = require("multer");
const fs = require("fs");
const moment = require("moment");
/*파일 업로드를 위한 Multer 사용 선언*/
function fileUpload(req, res) {
const filePath = "파일 저장 경로 선언"
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, `${filePath}`); // 파일이 저장되는 경로입니다.
},
filename: function (req, file, cb) {
cb(null, moment().format("YYYYMMDDHHmmss") + "_" + file.originalname); // 파일 저장 포멧
},
});
const upload = multer({ storage: storage }).single("file"); // single : 하나의 파일업로드 할때
upload(req, res, function (err) {
if (err instanceof multer.MulterError) {
} else if (err) {
}
getConnection((connection) => {
if (req.file) {
//JSON.parse()를 사용 안하면 "title":"JSONTILE","tag":"Tag" 이런식으로 사용가능
const jsonData = JSON.parse(req.body.jsonData);
const arrayData = req.body.arrayData;
console.log(jsonData);
console.log(arrayData);
console.log(req.file);
try {
//필요시 DB에 저장
res.send(true);
} catch (err) {
//에러 발생 파일 삭제 ex)DB에 insert 실패
if (fs.existsSync(req.file.path)) {
fs.unlinkSync(req.file.path);
}
logger.error(err);
res.send(false);
} finally {
connection.release();
}
} else {
res.send("Empty Files");
}
});
});
}
/*파일 업로드 업로드*/
router.post("/uploadFile", function (req, res) {
/*API 인증 모듈 웨어 작성 후*/
const auth = req.headers.authorization;
/*파일 업로드 함수 동작*/
fileUpload(req, res);
});
반응형
'Server' 카테고리의 다른 글
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- excel 파일 만들기 #node.js #express excel 파일 만들기 #데이터 입력해서 excel 파일 만들기
- React filter #js Includes #React Filter includes
- Pytorch #Yolov5 #Segementation
- Procedure #mysql #mysql Procedure #mysql 반복문 #Procedure 반복문 #mysql insert 반복문
- PoolCluster : Error: connect ECONNREFUSED 127.0.0.1:3306)
- Swal #sweetalert2 #alert #알림창 띄우기 #react swal
- see ec2 instance connect prerequisites at https://docs.aws.amazon.com/awsec2/latest/userguide #인스턴스 연결 안됨
- PDF #pdfkit
- ec2 instance connect is unable to connect to your instance. ensure your instance network settings are configured correctly for ec2 instance connect. for more information
- JavaScript #Programmers #lvl2 #프로그래머스 오픈채팅방 # 오픈채팅방 문제
- mysql date type
- react #react-spinners #modal loading #overlay #로딩창 #react 로딩창 만들기
- node.js #node.js pdf만들기 #node.js pdfkit
- mysql date
- BOJ #JS
- react #img 전송
- docker # docker build # m1 docker build
- mysql date between performance
- reack-cookies #아이디 저장하기 #react 아이디 저장 #react cookie #리엑트 아이디 저장하기
- Express multer #Express File 저장 #node.js
- docker mysql
- 이미지 전송 # 이미지 업로드 #이미지 여러장 #이미지 여러장 업로드 #react 이미지 업로드 #react 이미지 여러장 업로드
- FormData #FormData 파일전송 #FormData append json # React FormData File #React FormData append Json
- AWS #인바운드 #SSH #인스턴스 연결
- Mac docker.for.mac.host.internal
- supported: linux/amd64
- mysql date between # mysql date between 대소 비교 연산자
- node.js 파일 저장
- linux/amd64/v2
- ERROR: failed to solve: no support for running processes with linux/amd64/v3 platform
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함