nodejs/react安装XPI文件而不是下载
情况:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案:
需要重新设计后端和前端代码;
解决方案显然很简单。
1.backend:
不需要文件列表路由。
Express使用。
目标文件位于A public/目录中,在后端目录的根部创建。
app.js:
2。 frontend:
长话短说:声明您的组件,那么解决方案实际上只是html ...
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:
2. FRONTEND :
Long story short : declare your component then the solution is only HTML really...
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).