I made the ability to download files. The code works well on PC web browsers but not on mobile web browsers. I would appreciate it if you know how it works on mobile.
const filePath = findFile[0].path;
const fileName = findFile[0].filename;
const mimetype = mime.getType(filePath);
// 헤더 세팅
res.setHeader('fileName', encodeURIComponent(fileName));
res.setHeader('Content-type', mimetype);
res.setHeader('Content-Disposition', 'attachment; filename=' + encodeURIComponent(fileName));
// aws s3 파일
const params = { Bucket: 'flag-kog', Key: `${username}/${fileName}` };
s3.getObject(params)
.createReadStream()
.pipe(res);
componentDidMount() {
const { username, flagname } = this.props.match.params;
axios({
url: `/api/files/download/${username}/${flagname}`,
method: 'GET',
responseType: 'blob',
})
.then(res => {
const filename = decodeURIComponent(res.headers.filename);
this.setState({
filename,
});
// 다운로드
const url = window.URL.createObjectURL(new Blob([res.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', filename);
document.body.appendChild(link);
link.click();
this.setState({
downloading: true,
});
})
.catch(() =>
this.setState({
err: true,
})
);
}
Aucun commentaire:
Enregistrer un commentaire