如何通过response的头信息获取文件类型?

简介: 今天在前端工匠的群里,看到了一个问题(下载文件,但是请求头中需要传递 token,如何下载文件?怎么设置文件类型?),我们来解决一下这个问题。

下载文件方案



  1. (服务端)设置下载头 Content-disposition 。兼容性好。


Content-disposition: attachment
Content-Disposition: attachment; filename=abc.txt


  1. (前端)特性触发下载。移动端基本可以放弃,PC 端别考虑低版本 IE。


  1. dataurl,base64 设置下载头
  2. a 标签的 download 属性


如何处理鉴权相关逻辑?


  1. cookie。从 cookie 获取用户登录信息,然后判断是否有下载资源的权限。
    普普通通的方案。


  1. headers。从 headers 获取用户登录信息,然后判断是否有下载资源的权限。
    这种一般是 cookie 的升级,一般是一些防护 CSRF (跨站请求伪造)的系统架构。
    但是浏览器默认请求时无法使用,比如 location.href 等方式。


  1. url。从 url 获取权限信息,然后判断。
    感觉是最好的方案,比如说给一个key,有效期为五分钟,获取资源的时候直接判断。


和账号系统独立开,可以把资源放在第三方的系统。


浏览器 ajax
cookie 可以设置 可以设置
header 不可以设置 可以设置
url 可以设置 可以设置


因为他们的系统架构必须传递 header,所以只能用 ajax 这些浏览器下载方案了。


前端 ajax 下载文件


  1. 通过 ajax 请求资源


  1. 资源转换为 blob


  1. URL.createObjectURL(blob); 获取 bloburl


  1. 使用 download 属性,然后通过 click 触发下载。


function downloadFile(blob, fileName, ext){
    var a = document.createElement('a');
    a.href = URL.createObjectURL(blob);
    a.download = (fileName || Date.now()) + (ext?`.${ext}`:'');
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);// 兼容火狐
}


bVcHTKQ.webp.jpg


urls = [
    '//www.lilnong.top/static/html/_template.html',
    '//www.lilnong.top/static/css/normalize-8.0.0.css',
    '//www.lilnong.top/static/img/20190515/index-cir4.png',
    '//www.lilnong.top/static/fontmin/5d1e8557b8e311dae9ff17ee45563a27.svg',
    '//www.lilnong.top/static/fontmin/52ea7c14fd3e6a77587a50d251b0438a.ttf',
    '//www.lilnong.top/static/json/sitemap.json',
    '//www.lilnong.top/static/pdf/2018-ebook-engineer.pdf',
    '//www.lilnong.top/static/video/friday.mp4',
    '//www.lilnong.top/download/html/_template.html',
    '//www.lilnong.top/download/css/normalize-8.0.0.css',
    '//www.lilnong.top/download/img/20190515/index-cir4.png',
    '//www.lilnong.top/download/fontmin/5d1e8557b8e311dae9ff17ee45563a27.svg',
    '//www.lilnong.top/download/fontmin/52ea7c14fd3e6a77587a50d251b0438a.ttf',
    '//www.lilnong.top/download/json/sitemap.json',
    '//www.lilnong.top/download/pdf/2018-ebook-engineer.pdf',
    '//www.lilnong.top/download/video/friday.mp4'
]
urls.forEach(url=>{
    var xhr = new XMLHttpRequest();
    xhr.open('get', url)
    xhr.responseType = 'blob'
    xhr.send()
    xhr.onload = function(){
        downloadFile(xhr.response, '1'+ url)
    }
})
function downloadFile(blob, fileName, ext){
    var a = document.createElement('a');
    a.href = URL.createObjectURL(blob);
    a.download = (fileName || Date.now()) + (ext?`.${ext}`:'');
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);// 兼容火狐
}


获取 ajax 下载的文件类型


urls = [
    '//www.lilnong.top/static/html/_template.html',
    '//www.lilnong.top/static/css/normalize-8.0.0.css',
    '//www.lilnong.top/static/img/20190515/index-cir4.png',
    '//www.lilnong.top/static/fontmin/5d1e8557b8e311dae9ff17ee45563a27.svg',
    '//www.lilnong.top/static/fontmin/52ea7c14fd3e6a77587a50d251b0438a.ttf',
    '//www.lilnong.top/static/json/sitemap.json',
    '//www.lilnong.top/static/pdf/2018-ebook-engineer.pdf',
    '//www.lilnong.top/static/video/friday.mp4',
    '//www.lilnong.top/download/html/_template.html',
    '//www.lilnong.top/download/css/normalize-8.0.0.css',
    '//www.lilnong.top/download/img/20190515/index-cir4.png',
    '//www.lilnong.top/download/fontmin/5d1e8557b8e311dae9ff17ee45563a27.svg',
    '//www.lilnong.top/download/fontmin/52ea7c14fd3e6a77587a50d251b0438a.ttf',
    '//www.lilnong.top/download/json/sitemap.json',
    '//www.lilnong.top/download/pdf/2018-ebook-engineer.pdf',
    '//www.lilnong.top/download/video/friday.mp4'
]
urls.forEach(url=>{
    var xhr = new XMLHttpRequest();
    xhr.open('get', url)
    xhr.responseType = 'blob'
    xhr.send()
    xhr.onload = function(){
        //         console.log(url, xhr.response, xhr);
        console.log(url, xhr.response);
    }
})


bVcHTKf.webp.jpg


可以看到,我们设置 responseType = 'blob' 直接就可以通过 type 获取文件类型


通过解析 response 响应头获取类型


从 http 的响应头来分析,我们有两个地方可以获取。

  1. 通过 content-type 来获取
  2. 通过 Content-Disposition 来获取


urls = [
    '//www.lilnong.top/static/html/_template.html',
    '//www.lilnong.top/static/css/normalize-8.0.0.css',
    '//www.lilnong.top/static/img/20190515/index-cir4.png',
    '//www.lilnong.top/static/fontmin/5d1e8557b8e311dae9ff17ee45563a27.svg',
    '//www.lilnong.top/static/fontmin/52ea7c14fd3e6a77587a50d251b0438a.ttf',
    '//www.lilnong.top/static/json/sitemap.json',
    '//www.lilnong.top/static/pdf/2018-ebook-engineer.pdf',
    '//www.lilnong.top/static/video/friday.mp4',
    '//www.lilnong.top/download/html/_template.html',
    '//www.lilnong.top/download/css/normalize-8.0.0.css',
    '//www.lilnong.top/download/img/20190515/index-cir4.png',
    '//www.lilnong.top/download/fontmin/5d1e8557b8e311dae9ff17ee45563a27.svg',
    '//www.lilnong.top/download/fontmin/52ea7c14fd3e6a77587a50d251b0438a.ttf',
    '//www.lilnong.top/download/json/sitemap.json',
    '//www.lilnong.top/download/pdf/2018-ebook-engineer.pdf',
    '//www.lilnong.top/download/video/friday.mp4'
]
urls.forEach(url=>{
    var xhr = new XMLHttpRequest();
    xhr.open('get', url)
    xhr.responseType = 'blob'
    xhr.send()
    xhr.onload = function(){
        console.log(url, xhr.getAllResponseHeaders().match(/content-type: ([\w/]+)/g))
    }
})


bVcHTLL.webp.jpg

相关文章
|
PHP 调度 数据安全/隐私保护
【源码解读】TP5读取本地图片输出后,设置header头无效,图片乱码
在Thinkphp程序中读取本地图片,做出加工处理(如合并二维码等水印),然后输出给客户端,一直输出图片内容乱码。 设置了header image/png 不生效。 写下这篇TP源码排查文章,看看问题到底出现在哪个步骤。
536 0
【源码解读】TP5读取本地图片输出后,设置header头无效,图片乱码
|
语音技术
发送octet-stream格式的请求返回乱码处理
octet-stream格式的请求返回乱码处理
|
10月前
|
JSON 缓存 Java
修改Request与Response中的内容
修改Request与Response中的内容
78 0
|
JavaScript 前端开发 应用服务中间件
【已解决】“X-Content-Type-Options”头缺失或不安全
【已解决】“X-Content-Type-Options”头缺失或不安全
1095 0
POST请求body实现数据以记事本格式保存在服务器
POST请求body实现数据以记事本格式保存在服务器
50 0
如何读取文件格式头
如何读取文件格式头
|
JSON Java 数据格式
浏览器Header和cookie字符串形式转Json
浏览器Header和cookie字符串形式转Json
150 0
浏览器Header和cookie字符串形式转Json
|
JavaScript PHP
文件bom头,文件bom头保存的什么东西,php读取bom头数据
文件bom头,文件bom头保存的什么东西,php读取bom头数据
82 0
通过二进制头识别文件类型
通过二进制头识别文件类型,可以使用UE或者WinHex软件打开 1.JPEG/JPG - 文件头标识 (2 bytes): $ff, $d8 (SOI) (JPEG文件标识)  - 文件结束标识 (2 bytes): $ff, $d9 (EOI)  2.
1929 0
|
JavaScript 开发工具 前端开发