0
我有和API鏈接自動開始下載文件,如果我在地址欄中按照它。我們稱之爲my-third-party-download-link-com。Express res.redirect顯示內容但不下載
但是,在Express框架中,我設置res.redirect(my-third-party-downloading-link-com)。我得到狀態代碼301,並可以在開發人員工具中看到預覽標籤中的文件內容。但是我無法讓瀏覽器下載這個文件。
我適當的請求處理程序如下:
downloadFeed(req, res) {
const { jobId, platform, fileName } = req.query;
const host = platform === 'production' ? configs.prodHost :
configs.stageHost;
const downloadLink = `${host}/api/v1/feedfile/${jobId}`;
// I also tried with these headers
// res.setHeader('Content-disposition', 'attachment;
// filename=${fileName}.gz');
// res.setHeader('Content-Type', 'application/x-gzip');
res.redirect(downloadLink)
}
PS現在,爲了解決這個問題,我建立我的第三方的下載鏈接-COM在後端,發送與res.end然後:
window.open(**my-third-party-downloading-link-com**, '_blank').
但我不喜歡這個解決方案。我該如何告訴瀏覽器開始從這個第三方API下載內容?
你是不對的。我們正在談論第三方鏈接,而不是關於服務器文件系統上的文件。 –
好的,那麼你可以檢查這個問題,這可能會有所幫助https://stackoverflow.com/questions/26288055/how-to-send-a-file-from-remote-url-as-a-get-response -in節點-JS-EXPRESS-應用 –