簡體   English   中英

NodeJS/React 安裝 xpi 文件而不是下載它

[英]NodeJS/React install xpi file instead of downloading it

情況:

1.我的nodeJS服務器提供這樣的文件:

fileRouter.get('/firefox',  async (req,res)=>{
        const mime = 'application/x-xpinstall'
        fs.readFile('controllers/file.xpi', (err, data)=>{
                if(err){
                        res.writeHead(500, {'Content-Type' : 'text-plain'})
                        return res.end('error while downloading the file')
                }
                res.writeHead(200, {'Content-Type' : mime})
                res.end(data)
        }
        )
 

})

2.我的反應應用程序像這樣下載它:

const handleDownload = async (e) =>{
    const res = await axios({
        url:'/api/download/firefox',
        method:'GET',
        responseType:'blob'
    })
    const url = window.URL.createObjectURL(new Blob([res.data]))
    const link = document.createElement('a')
    link.href = url
    document.body.appendChild(link)
    link.click()
}

問題:

我希望 xpi 擴展文件由 firefox 安裝而不是下載。 我認為在我的節點服務器中設置 mime 類型會導致來自 firefox 的此類行為。

InstallTrigger 已棄用,並且在 mozilla 文檔中未提及。

我認為問題出在前端代碼上:我應該更改什么? (我什至對實現下載的方式都不滿意,我一定錯過了一些東西)

謝謝你的幫助。

解決方案:

后端和前端代碼都需要重新編寫; 解決方案顯然很簡單。

1.后端:

fileRouter 路由不再是必需的。 使用快遞。 目標文件位於在后端目錄的根目錄下創建的public/目錄中。

應用程序.js:

express.static.mime.define({'application/x-xpinstall' : ['xpi']})
app.use('/download' , express.static('public'))


2.前端:

長話短說:聲明你的組件然后解決方案只是 HTML 真的......

const Download = () =>{
return(
    <a href='https://yourwebsite.com/download/yourfirefoxextension.xpi'>
        download the extension
    </a>
)
}

后記:

用戶接受后通過firefox直接安裝擴展。 無需設置 express.Router()。 我仍然很想知道我嘗試的第一種方法是否有機會工作(如果您覺得自己有答案,請不要猶豫)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM