使用html5下载保留名称下载dotfile [英] Download dotfile using html5 download preserving name

查看:85
本文介绍了使用html5下载保留名称下载dotfile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录结构,其中包含目录和文件的列表.我想给用户一个下载文件的选项.对于下载,我使用的是 HTML5下载属性.效果很好.

但是我拥有的目录结构也可以包含点文件,例如: .babelrc .gitignore .eslintrc 等.

当我使用相同的技术下载此类文件时,正在下载具有相同内容的文件,但该文件不再是点文件.下载后,假设为 .gitignore ,文件将变为 gitignore.txt .

我正在将其用于我的项目 github-plus -Chrome扩展程序显示每个文件的大小,下载链接以及复制其内容的选项.

任何帮助将不胜感激.

我正在使用以下格式:< a href ="https://raw.githubusercontent.com/softvar/secure-ls/master/.babelrc" title =下载文件" download =.babelrc">下载</a>

JSFIDDLE DEMO

解决方案

下载资源 ,关于文件类型/扩展名:

如果声明的类型已知,则更改文件名以添加与声明的类型相对应的扩展名.

否则,如果已知命名类型具有潜在危险(例如,平台约定会将其视为本机可执行文件,shell脚本,HTML应用程序或具有可执行宏功能的文档),则可以选择更改文件名以添加已知安全的扩展名(例如".txt").

似乎:

  • 最终选择文件名的算法部分是与平台有关
  • 如果未识别扩展名(如 dotfiles 的情况),浏览器将尝试使用文件MIME类型来确定扩展名
  • 由于
  • dotfiles 可能是潜在有害,因为它们是各种平台上的隐藏文件.这似乎是您的情况,删除了初始点并附加了 .txt 扩展名.

I have a directory structure, containing a list of directories and files. I want to give user an option of downloading a file. For downloading, I'm using HTML5 download attribute. It works perfectly.

But the directory structure i have can have dotfiles too, examples: .babelrc, .gitignore, .eslintrc, etc.

When I use the same technique to download such files, file is being downloaded with the same content but the file is no longer a dotfile. After downloading, let's say .gitignore, the file becomes gitignore.txt.

I'm using this for my project github-plus - Chrome extension to display size of each file, download link and an option of copying it's contents.

Any help would be highly appreciated.

I'm using this format: <a href="https://raw.githubusercontent.com/softvar/secure-ls/master/.babelrc" title="Download File" download=".babelrc">Download</a>

JSFIDDLE DEMO

解决方案

Quoting HTML5 specification on downloading resources with the download attribute, about file type/extension :

If the claimed type is known, then alter filename to add an extension corresponding to claimed type.

Otherwise, if named type is known to be potentially dangerous (e.g. it will be treated by the platform conventions as a native executable, shell script, HTML application, or executable-macro-capable document) then optionally alter filename to add a known-safe extension (e.g. ".txt").

It seems that:

  • the part of the algorithm that finally choses the filename is platform-dependent
  • if the extension is not recognised, as in the case of dotfiles, the browser will try to determine it by using the file MIME type
  • dotfiles might be considered anyway as potentially harmful as they are hidden files on various platforms. This seems to be what happens in your case, with the initial dot being removed and the .txt extension appended.

这篇关于使用html5下载保留名称下载dotfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆