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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@sunzan/universal-file-dl

v0.1.0

Published

通用文件流导出工具:自动解析文件名,兼容 fetch/axios,支持任意格式

Downloads

0

Readme

文件流导出工具

通用浏览器端下载工具,兼容任意格式(以服务端返回为准),支持:

  • 自动解析 Content-Disposition 得到文件名(兼容 filename* UTF-8 与 filename
  • 适配 fetchaxios 响应对象
  • 直接传入 Blob/ArrayBuffer/Uint8Array 下载

安装/放置

可以直接复制 src/utils/fileDownload.js,或通过 npm 安装:

npm i universal-file-download
# 或
pnpm add universal-file-download

使用:

import downloader, { downloadByFetch, downloadFromAxiosResponse } from 'universal-file-download';

API

  • resolveFilename(headers, fallback?, url?):从响应头/URL 推断文件名
  • downloadFromFetchResponse(response, { filename? }):基于 fetch Response 下载
  • downloadFromAxiosResponse(axiosResponse, { filename? }):基于 axios 响应下载(需设置 responseType
  • downloadByFetch(url, fetchInit?):直接传入 URL,用 fetch 请求并下载
  • downloadFromData(data, { filename, type? }):从原始数据(Blob/ArrayBuffer/Uint8Array)下载

用法示例

1) 使用 axios

import axios from 'axios';
import downloader from './src/utils/fileDownload';

async function exportWithAxios() {
  const res = await axios.get('/api/export', {
    responseType: 'blob', // 或 'arraybuffer'
    params: { q: 'keyword' },
  });

  await downloader.downloadFromAxiosResponse(res, { filename: 'fallback.xlsx' });
}

2) 使用 fetch(已拿到 Response)

import { downloadFromFetchResponse } from './src/utils/fileDownload';

async function exportWithFetchResponse() {
  const res = await fetch('/api/export', { credentials: 'include' });
  await downloadFromFetchResponse(res, { filename: 'fallback.csv' });
}

3) 使用 fetch 直接请求并下载

import { downloadByFetch } from './src/utils/fileDownload';

async function exportByUrl() {
  await downloadByFetch('/api/export?type=zip', { filename: 'fallback.zip' });
}

4) 从原始数据下载

import { downloadFromData } from './src/utils/fileDownload';

async function exportFromData() {
  const data = new Uint8Array([1,2,3,4]);
  downloadFromData(data, { filename: 'demo.bin', type: 'application/octet-stream' });
}

服务器返回建议

  • 设置正确的 Content-Type,例如 application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  • 设置 Content-Disposition
    • attachment; filename="report.xlsx"
    • attachment; filename*=UTF-8''%E6%8A%A5%E8%A1%A8.xlsx

注意事项

  • Safari/部分浏览器需要使用 Blob URL 触发下载,工具已处理
  • 若服务端返回 JSON 错误体而非二进制,请在触发下载前根据项目约定先判断响应是否为文件(例如检查 content-type 是否包含 application/json),再做错误处理

演示页运行

无需构建工具,直接本地打开 demo 页面:

  1. 用本机静态服务器(推荐)
npx http-server . -p 8080
# 或者:python3 -m http.server 8080

浏览器访问 http://localhost:8080/demo/,点击三个按钮进行测试。

  1. 直接 file:// 打开

双击打开 demo/index.html 也可运行,但某些浏览器对 module + 相对路径在 file:// 下有安全限制,建议使用本机静态服务器。