I have to download large file(~5GB) from ubuntu server in an ec2 instance to Unity WebGl(Web Browser).
In Ubuntu server, I use NodeJS, express.
I was able to access files on the server through restapi (unity web request) and read file data successfully.
But, I can't download this data to file to local client(User PC).
I found how to download file from server like these.
- How To Work with Files Using Streams in Node.js : https://www.digitalocean.com/community/tutorials/how-to-work-with-files-using-streams-in-node-js
- Download a file from NodeJS Server using Express : Download a file from NodeJS Server using Express
- File Upload and Download in Node.js using Multer : https://medium.com/geekculture/file-upload-and-download-in-node-js-c524a8050c8f
They all say it can use
res.setHeader('Content-disposition', 'attachment; filename=' + filename);
res.setHeader('Content-type', mimetype);
var filestream = fs.createReadStream(file);
filestream.pipe(res);
or
res.download(file);
So, I wrote the code for the download as follows.
const doGetDownloadFile = (req, res, next) => {
var upload_folder = './uploads';
//var file = upload_folder + '/' + req.body.fileName;
var file = upload_folder + '/' + 'logs.txt';
try {
if (fs.existsSync(file)) { // 파일이 존재하는지 체크
var filename = path.basename(file); // 파일 경로에서 파일명(확장자포함)만 추출
var mimetype = mime.getType(filename); // 파일의 타입(형식)을 가져옴
console.log(filename, mimetype);
console.log(getDownloadFilename(req, filename));
// res.download(file);
res.setHeader('Content-Disposition', 'attachment; filename=' + getDownloadFilename(req, filename));
res.setHeader('Content-type', mimetype);
var filestream = fs.createReadStream(file);
//filestream.on("data", () => process.stdout.write('.')).pipe(res);
var stat = fs.statSync(file);
var str = progress({
length: stat.size,
time: 100
});
str.on('progress', function(progress) {
console.log(progress.percentage);
});
filestream.pipe(str).pipe(res).on('error', (err) => {
console.log("Error Log In Server : ", err);
}).on('finish', ()=> {
console.log("Finish Log in Server");
})
} else {
res.send('해당 파일이 없습니다.');
return;
}
} catch (e) { // 에러 발생시
console.log(e);
res.send('파일을 다운로드하는 중에 에러가 발생하였습니다.');
return;
}
}
This code works without errors, but there is a problem.
The file download percentage is increasing, but when I look at the response tab in the network window, there is nothing, and as a result, only the percentage is increasing, and I don't actually download the file from my browser.
I'm a web beginner, so I don't know much. What should I do now?