npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

browser-file-utils

v0.1.19

Published

A browser file utils

Downloads

26

Readme

browser-file-utils

1、简介

浏览器文件操作方法集合

2、作用

包含很多文件操作方法

3、如何使用

3.1 安装

npm i browser-file-utils

3.2 使用方法

引入

import fileUtils from 'browser-file-utils'

or 

import {
    getBaseName,
} from 'browser-file-utils'

获取无后缀名的文件名

import {
    getBaseName,
} from 'browser-file-utils'
console.log(getBaseName('user-avater.png')) // user-avater
console.log(getBaseName('user-avater.jpg.png')) // user-avater.jpg

获取文件名的后缀名

import {
    getExtension,
} from 'browser-file-utils'
console.log(getExtension('user-record-list.docx')) // docx

判断文件是否为图片

import {
    isImage,
} from 'browser-file-utils'
console.log(isImage('a.b.c.docx')) // false
console.log(isImage('user-avater.png')) // true

计算文件大小

import {
    countMB,
} from 'browser-file-utils'
console.log(countMB('1024')) // 1.00 KB
console.log(countMB('5325886')) // 5.08 MB
console.log(countMB('5656545484')) // 5.27 GB
console.log(countMB('5656545484')) // 5.14 TB

图片元素对象转换为base64字符串

import {
    image2Base64,
} from 'browser-file-utils'

let image = document.createElement('img')
image.src = './demo/WX20210513-091307.png'
document.body.appendChild(image)
image.onload = function () {
    let imageBase64 = image2Base64(image)
    console.log(imageBase64)
}

服务器图片转为base64

import {
    getImgBase64,
} from 'browser-file-utils'

getImgBase64('./demo/WX20210513-091307.png', (imgBase64) => {
    console.log(imgBase64)
})

base64 转为 blob对象

import {
    getImgBase64,
    base64ToBlob,
} from 'browser-file-utils'

getImgBase64('./demo/WX20210513-091307.png', (imgBase64) => {
    console.log(imgBase64)
    // base64转换为 blob对象
    let imageBlob = base64ToBlob(imgBase64)
    console.log(imageBlob)
})

blob对象转为file对象

import {
    getImgBase64,
    base64ToBlob,
    blob2File,
} from 'browser-file-utils'

getImgBase64('./demo/WX20210513-091307.png', (imgBase64) => {
    console.log(imgBase64)
    // base64转换为 blob对象
    let imageBlob = base64ToBlob(imgBase64)
    console.log(imageBlob)
    // 将blob对象转为file对象
    let fileObject = blob2File(imageBlob, 'test.png')
    console.log(fileObject)
})

通过input文件域获取到的图片file对象转为base64

import {
    getImgBase64,
    base64ToBlob,
    blob2File,
    imageFileToBase64,
} from 'browser-file-utils'

getImgBase64('./demo/WX20210513-091307.png', (imgBase64) => {
    console.log(imgBase64)
    // base64转换为 blob对象
    let imageBlob = base64ToBlob(imgBase64)
    console.log(imageBlob)
    // 将blob对象转为file对象
    let fileObject = blob2File(imageBlob, 'test.png')
    console.log(fileObject)
    // 将一个image file对象转为base64
    imageFileToBase64(fileObject, (res) => {
        // res = { error, data }
        let imgTag = document.createElement('img')
        imgTag.src = res.data
        document.body.appendChild(imgTag)
    })
})

下载文件到本地

import {
    downloadFile
} from 'browser-file-utils'

downloadFile('tt.txt', 'http://127.0.0.1:8080/demo/tt.txt')
downloadFile('WX20210513-091307.png', 'http://127.0.0.1:8080/demo/WX20210513-091307.png')