1. file-saver 모듈 다운로드
npm i --save file-saver
https://www.npmjs.com/package/file-saver
file-saver
An HTML5 saveAs() FileSaver implementation
www.npmjs.com
2. Angular (Client) 부분
let headers = new HttpHeaders({
"Authorization": "Bearer " + user.jwt, // Auth header
//No other headers needed
});
this.http
.get("/download/dump", { headers, responseType: "blob" }) //set response Type properly (it is not part of headers)
.toPromise()
.then(blob => {
saveAs(blob, "dump.gz");
})
.catch(err => console.error("download error = ", err));
3. Node.js (Server) 부분
app.get( "/download/dump", authenticate, (req:Request, res:Response) => {
const file = path.resolve(__dirname, `./dumps/dump.gz`);
//No need for special headers
res.download(file);
})
출처
https://stackoverflow.com/questions/56219373/node-angular-download-a-file-with-http-request
Node + Angular : download a file with HTTP request
I tried 400 combinations of syntaxes and headers, I can't figure out how to make a HTTP call from Angular to retrieve a file from my NodeJS server. Found on Stackoverflow and tried, to no avail :
stackoverflow.com
http://expressjs.com/ko/api.html#res.download
Express 4.x - API 참조
Express 4.x API express() Creates an Express application. The express() function is a top-level function exported by the express module. var express = require('express') var app = express() Methods express.json([options]) This middleware is available in Ex
expressjs.com
'Programming > JavaScript & TypeScript' 카테고리의 다른 글
[Node-MongoDB] 인증(Authentication)이 추가된 MongoDB Client에 접속하기 (0) | 2020.06.09 |
---|---|
[Webpack] 모듈 신규 설치 후 build 파일 실행 시 에러 (0) | 2020.04.08 |
Javascript 배열 정렬, sort() (0) | 2019.10.25 |
Angular7 ag-Grid 도입 (0) | 2019.09.26 |
[Node Express-Angular 7] 브라우저 새로 고침 시 404 에러 (0) | 2019.07.24 |