通過 Node 批量下載檔案到本地(多檔案)
最近在做一個檔案下載的功能,這裡把做的過程中用的技術和坑簡要總結下。
上篇文章《通過 JavaScript 下載檔案到本地(單檔案)》 說了下如何下載單檔案,這篇主要說下如何做多檔案的批量下載
多檔案分別處理
如果檔案數量可控,對於下載出來的檔案格式無要求,可以用最簡單的辦法,直接遍歷檔案,分別給每個下載連結建立一個單檔案的download或者iframe下載連結。
zip包批量下載
雖然說可以遍歷所有檔案,然後去批量下載單個檔案,但是這種體驗畢竟不太好,最常見的做法是把批量的檔案下載並打包到zip中。
所以首先的一個實現思路是:在代理服務裡,先去遍歷所有的檔案去請求檔案資料,然後壓縮到zip包中,然後再把zip包返回給客戶端。
這麼做對於下載量資料比較小時ok,但是如果批量檔案特別多特別大時,使用者要等後臺把所有的資料都請求到並且都打包都壓縮包裡,前端才能有反饋,這個時間可能會耗時很長,使用者體驗可能很差。
在同事的前期調研時,有說這裡可以做一個流式的邊壓縮邊下載
的能力,大致的思路是,chunk回包,加流式壓縮
。
...... let fileCounter = 0; const zippedFilename = encodeURIComponent(downloadData.name); const list = downloadData.list || []; const header = { 'Content-Type': 'application/x-zip', 'Pragma': 'public', 'Expires': '0', 'Cache-Control': 'private, must-revalidate, post-check=0, pre-check=0', 'Content-disposition': 'attachment; filename="' + zippedFilename + '"', 'Transfer-Encoding': 'chunked', 'Content-Transfer-Encoding': 'binary' }; res.writeHead(200, header); archive.store = true; archive.pipe(res); list.map(item => { fileCounter++; let inStream = request.get(item.downLoadUrl); let name = item.fileName; let length = 0; inStream.on('response', function(awsData) { archive.append(inStream, { name: name }); }).on('data', function(data) { length += data.length; }).on('error', function(e) { console.error(name + '-error', e); }).on('end', function(endData) { fileCounter--; if (fileCounter < 1) { archive.finalize(); } }); }); archive.on('error', function(err) { throw err; }); archive.on('finish', function(err) { return res.end(); }); ......
當然中間還有些細節需要處理:比如中文檔名的問題,是否需要下載檔案總大小做限制,是否會出現檔案不存在等等情況。