nodejs/react安装XPI文件而不是下载

发布于 2025-01-25 10:40:38 字数 1193 浏览 3 评论 0原文

情况:

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.我的React应用程序下载它:

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()
}

问题:< /strong>

我希望XPI扩展文件将由Firefox安装而不是下载。我认为在我的节点服务器中设置MIME类型会导致Firefox的这种行为。

安装触发器已被弃用,Mozilla文档中没有提及。

我认为问题在于前端代码:我应该更改什么? (我什至不满意下载的实现方式,我必须在那里错过一些东西)

感谢您的帮助。

Situation :

1.My nodeJS server serves a file like so :

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 :

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. I thought setting the mime type in my node server would lead to such behaviour from firefox.

InstallTrigger is deprecated and isn't mentionned in mozilla documentation.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失与倦" 2025-02-01 10:40:38

解决方案:

需要重新设计后端和前端代码;
解决方案显然很简单。

1.backend:

不需要文件列表路由。
Express使用。
目标文件位于A public/目录中,在后端目录的根部创建。

app.js:

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


2。 frontend:

长话短说:声明您的组件,那么解决方案实际上只是html ...

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

postscript:

用户接受后,Firefox直接安装了扩展名。
无需设置Express.Router()。
我仍然很想知道我尝试的第一种方式是否有机会工作(如果您觉得自己有答案,请不要犹豫)。

Solution :

Both the backend and frontend code needed to be reworked;
The solution is clearly simple.

1.BACKEND :

The fileRouter Route is no more necessary.
Express is used.
Targeted file is in a public/ directory created at the root of the backend directory.

app.js:

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


2. FRONTEND :

Long story short : declare your component then the solution is only HTML really...

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.
No need for setting-up an 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文