file-loads

(携带头信息)资源的预览和下载

Usage no npm install needed!

<script type="module">
  import fileLoads from 'https://cdn.skypack.dev/file-loads';
</script>

README

file-loads

有些时候获取一个资源,请求地址的同时需要携带一些头信息如“token”或者其他,这时候可以使用 file-loads 轻易的实现,资源的预览或下载

( Sometimes you need to take some header information such as "token" or other when you request the address of a resource. You can use file-loads to easily implement, preview or download the resource. )

源码地址(Source address)

安装(Install)

npm install file-loads --save

使用(Usage)

方法(Method)

openFile( type [ , filename] )( url [ , headers] )

参数(Arguments)

type:<String> 类型“open”预览,“download”下载,默认“open”

filename:<String>(可选)文件名

url:<String> 资源地址

headers:<String/Object>(可选)头信息,传token:String默认{authorization:token}

例(example)

import {openFile} from 'file-loads'
/* 类型 open: 预览, download: 下载 */
const type = 'open'
/* 资源地址 */
const url = '/'
/* 设置请求头 接受string和object, string 会转为 {authorization:string} */
const headers = 'token'
/* 文件名 如有类型区分 可省略扩展名,可选 ,默认先从返回头 content-disposition 中取,取不到则为时间戳 */
const filename = '1.jpg'

/* 预览 */
openFile('open')(url,headers)

/* 下载 */
openFile('download', filename)(url, headers)

方法(Method)

uploadFile( url , file [ , headers] )

参数(Arguments)

url:<String> 资源地址

file:<File> 资源,可以从<input type="file">获取

headers:<Object>(可选)头信息

例(example)

import {uploadFile} from 'file-loads'
/* 需要上传的file */
const file = file
/* 上传地址 */
const url = url
/* 设置请求头 */
const headers = {
    authorization: 'token'
}
/* 调用utils.uploadFile */
const result = await uploadFile(url, file, headers)

方法(Method)

asyncToDataUrl( file )

参数(Arguments)

file:<File> 资源,可以从<input type="file">获取

例(example)

import {asyncToDataUrl} from 'file-loads'
/* 转Base64Url */
const url = await asyncToDataUrl(file)