简体   繁体   English

NodeJS/React 安装 xpi 文件而不是下载它

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

Situation:情况:

1.My nodeJS server serves a file like so: 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.My react app downloads it like so: 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()
}

Problem:问题:

I expected the xpi extension file to be installed by firefox instead of being downloaded.我希望 xpi 扩展文件由 firefox 安装而不是下载。 I thought setting the mime type in my node server would lead to such behaviour from firefox.我认为在我的节点服务器中设置 mime 类型会导致来自 firefox 的此类行为。

InstallTrigger is deprecated and isn't mentionned in mozilla documentation. InstallTrigger 已弃用,并且在 mozilla 文档中未提及。

I think the problem lies in the frontend code: what should I change?我认为问题出在前端代码上:我应该更改什么? (I'm not even satisfied with the way downloading is implemented, I must miss something there) (我什至对实现下载的方式都不满意,我一定错过了一些东西)

Thanks for your help.谢谢你的帮助。

Solution:解决方案:

Both the backend and frontend code needed to be reworked;后端和前端代码都需要重新编写; The solution is clearly simple.解决方案显然很简单。

1.BACKEND: 1.后端:

The fileRouter Route is no more necessary. fileRouter 路由不再是必需的。 Express is used.使用快递。 Targeted file is in a public/ directory created at the root of the backend directory.目标文件位于在后端目录的根目录下创建的public/目录中。

app.js:应用程序.js:

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


2. FRONTEND: 2.前端:

Long story short: declare your component then the solution is only HTML really...长话短说:声明你的组件然后解决方案只是 HTML 真的......

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

Postscript:后记:

The extension is directly installed by firefox after the user accepts it.用户接受后通过firefox直接安装扩展。 No need for setting-up an express.Router().无需设置 express.Router()。 I'm still curious to know if the first way I tried had any chance to work (if you feel you have the answer, don't hesitate).我仍然很想知道我尝试的第一种方法是否有机会工作(如果您觉得自己有答案,请不要犹豫)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM