制作一个 Chrome 扩展来下载(而不是查看)链接并使用自定义文件名
我目前正在为 Chrome 开发一个简单的 javascript 扩展。问题很简单,但可能无法修复,但问题仍然存在:
在特定页面上,我嵌入了一些文件的链接(我无权访问服务器,所以我无法更改它们的输出方式) - 一个简单的 < code>nchor 标签。 然后,我想要实现的是:
设置下载文件的文件名(现在就像
87sfhkjhsf
并且没有扩展名)使浏览器在任何情况下都下载(而不是查看)文件(现在,如果它是pdf,它会被查看..或者如果它是mp3使用本机 Chrome 播放器播放)
我什至会接受 .dll 解决方案,如果它确实有效(我什至尝试使用 Flash 和 FileReference.download 方法,但它不允许从其他域下载文件)
寻求你们的帮助:)
I'm currently developing a simple javascript extension for Chrome. The problem is simple but probably unfixable, but still here's the issue:
on particular pages, i embed links to some files (i don't have access to server, so i can't change the way they are outputted) - a simple <a>
nchor tag.
then, what i'm trying to achieve is:
set a filename of downloaded file (right now it's like
87sfhkjhsf
and without extension)make the browser download (not view) the file in any case (right now, if it's a pdf, it gets viewed.. or if it's an mp3 it gets played with native Chrome player)
i would even accept a .dll solution IF it will really work (i even tried using Flash with FileReference.download method, but it doesn't allow to download files from other domains)
seeking help from you guys :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要在 chrome 中使用 javascript 以编程方式下载文件,您(当前)需要服务器来反弹下载,以满足乱码文件名(使用 blobbuilder;这听起来像您正在做的事情),或者 NPAPI 插件(例如作为自定义 .dll 或 Flash)。
如果您查看屏幕截图扩展,您会发现最流行的扩展使用 NPAPI。
我计划自己使用 NPAPI,但还没有抽出时间为所有 3 个操作系统进行编码。 (我是 Smooth Gestures 的开发者,收到了很多图像下载手势的请求)
综上所述,JavaScript 的文件系统访问在不断发展,并且本机 API 可能会变得可用。但我不会屏住呼吸。
In or to programmatically download a file using javascript in chrome you (currently) need either a server to bounce the download off, to be satisfied with garbled filenames (using blobbuilder; this sounds like what you are doing), or a NPAPI plugin (such as a custom .dll or Flash).
If you look at the screenshot extensions, the most popular ones use NPAPI.
I plan on using NPAPI myself, but haven't gotten around to coding for all 3 OSes yet. (I am the developer of Smooth Gestures, and have gotten many requests for a image download gesture)
All this said, there is constant development on filesystem access for javascript and a native API may become available. But I wouldn't hold my breath.