Mi Lugarcito

[포스트맨] 파일업로드 & multipart/form-data 본문

Express.js

[포스트맨] 파일업로드 & multipart/form-data

selene park 2022. 8. 7. 17:43

/**
 * POST /admin/inquiry-management/inquiry-consult - 상담 내용 생성하기
 * @param {Integer} inquiryId
 * @param {String} schedule
 * @param {String} address
 * @param {String} menu
 * @param {String} quantity
 * @param {String} print
 * @param {String} design
 * @param {String} powerSupport
 * @param {String} etc
 * @param {String} amount
 * @param {String} quotationFileUrl

// ** 사진파일 첨부한경우
// headers : "Content-Type" : "multipart/form-data" 
// body : form-data 
// "consultInfo" : {"inquiryId":1, "schedule":"schedule test", "address":"address test", "menu":"menu test", "quantity":"quantity test", "design":"design test", "powerSupport":"powerSupport test", "etc":"etc test", "amount":"amount test"}


// headers : "Content-Type" : "application/json" 
// raw body + body : raw
"consultInfo": "{\"inquiryId\":1, \"schedule\":\"schedule test\", \"address\":\"address test\", \"menu\":\"menu test\", \"quantity\":\"quantity test\", \"design\":\"design test\", \"powerSupport\":\"powerSupport test\", \"etc\":\"etc test\", \"amount\":\"amount test\" }"

*/
    async createInquiryConsult(req, res, next) {
        const transaction = await sequelize.transaction();
        try {
            let consultInfo = JSON.parse(req.body.consultInfo);
            const quoteFile = req.files['quote-file'] || [];
            
            if(quoteFile.length > 0){
                consultInfo.quotationFileUrl = quoteFile[0].key;
            }

            await InquiryConsult.create(consultInfo);

            await transaction.commit();
            return  responseWrapper(res);

        } catch (e) {
            console.error(`createInquiryConsult Error : ${e}`);
            await transaction.rollback();
            return next(e);
        }
    },