사용자가 나중에 데이터베이스에 저장되는 csv 파일을 가져올 수있는 redux-react 앱을 만들려고합니다. 지금은 사용자가 컴퓨터에서 다운로드하려는 csv 파일을 선택한 다음 파일을 백엔드로 보낼 수있는 코드를 생성하려는 프런트 엔드에서 작업하고 있습니다. 따라서 csvReader를 사용하여 csv 파일을 읽었지만 데이터를 백엔드로 보내는 방법을 모르겠습니다 . 백엔드에서 nestJS를 사용하고 있습니다. 한 번에 전체 csv 파일을 보내고 싶지만 문제를 해결하는 방법을 모르겠습니다. 나는 초보자입니다 :))) 내 문제를 해결하는 방법을 알고 있습니까?
반응을 도와 드릴 수는 없지만이 NestJS 부분이 도움이 될 수 있습니다. multer 를 사용 하여 API를 구성하고 상점 경로를 설정할 수 있습니다.
멀터 옵션 생성
// multer.ts
const excelMimeTypes = [
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/wps-office.xlsx',
'application/vnd.ms-excel',
];
export const multerOptions = {
fileFilter: (req: any, file: any, cb: any) => {
const mimeType = excelMimeTypes.find(im => im === file.mimetype);
if (mimeType) {
cb(null, true);
} else {
cb(new HttpException(`Unsupported file type ${extname(file.originalname)}`, HttpStatus.BAD_REQUEST), false);
}
},
storage: diskStorage({
destination: (req: any, file: any, cb: any) => {
const uploadPath = '/upload'; // use env var
if (!existsSync(uploadPath)) {
mkdirSync(uploadPath); // create if not exists
}
cb(null, uploadPath);
},
filename: (req: any, file: any, cb: any) => {
cb(null, file.originalname);
},
}),
};
최근에 생성 된 multerOption을 가져오고 FileInterceptor 및 UploadedFile 데코레이터를 사용하여 파일을 가져옵니다.
@Post()
@UseInterceptors(FileInterceptor('file', multerOptions))
uploadFile(@UploadedFile() file) {
console.log(file) // call service or whathever to manage uploaded file.. handleFile in the example below..
}
xlsx 라이브러리를 사용하여 파일 (예제)을 관리합니다 .
handleFile(file: any): Promise<any> {
return new Promise(async (resolve: (result: any) => void, reject: (reason: any) => void): Promise<void> => {
try {
const workbook = XLSX.readFile(`${uploadLocation}/${file.filename}`);
resolve(workbook.Sheets[sheetName]);
} catch (error) {
reject(error);
}
});
}
도움이 되었기를 바랍니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다