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

js-file-saver

v1.0.0

Published

A multi-browser-compatible download and export file tool

Downloads

4

Readme

introduce

介绍

js-file-saver 是一个兼容多浏览器的下载、导出文件工具。

下载、导出的数据通过API接口返回,服务端需在对应接口需正确设置消息头 response header

例如:

Content-Disposition: attachment; filename="filename.jpg"

attachment 意味着消息体应该被下载到本地;大多数浏览器会呈现一个“保存为”的对话框,将filename的值预填为下载后的文件名。

使用

browser

(仅下载至本地后使用,不能直接作为 CDN 资源引用)

unzipped

(~zipped)

<script src="js-file-saver.js"></script>
var fileSaver = new JsFileSaver(options)

npm

通过 npm

npm install --save js-file-saver
import JsFileSaver from 'js-file-saver';

const fileSaver = new JsFileSaver(options);

API 说明文档

构造函数

JsFileSaver()

Arguments

  • options {Object} 下载设置

Returns

Example

import JsFileSaver from 'js-file-saver';

// 生成实例(这里的参数设置主要是为了在用户环境不支持 Blob,Promise 等情况下的兼容下载)
const downloadBook = new JsFileSaver({
  // 下载接口地址:'/api/downloadBook' 或 'http://localhost:3001/downloadBook'
  action: '/api/downloadBook', 
  // 下载接口类型:'get' | 'post'。默认为:'post'
  method: 'post', 
  // 接口Content-Type设置,默认为:'application/x-www-form-urlencoded'。另可选:'multipart/form-data' | 'text/plain'
  contentType: 'application/x-www-form-urlencoded', 
  // 查询参数
  payload: {
    bookId: 1,
  },
});

下载(保存)文件

saver()

Arguments

  • httpResponse {Object} http 响应体
  • httpResponse.data {ArrayBuffer | ArrayBufferView | Blob | String} 文件数据
  • httpResponse.header {String} 文件信息

Returns

Example

import axios from 'axios';
import DL from 'js-file-saver';

const downloadBook = new DL({
  action: '/api/downloadBook', 
  method: 'post', 
  payload: {
    bookId: 1,
  },
});

// 发出下载请求(可使用任意方式完成接口交互,这里使用 axios。也可使用 jQuery ajax,fetch 等)。
axios.request({
  url: '/api/downloadBook',
  method: 'POST',
  data: {
    bookId: 1,
  },
}).then((response) => {
  // 执行下载操作
  downloadBook.saver(response);
});

兼容下载(保存)文件

js-file-saver 默认已有对异常的处理:在浏览器不支持 BlobPromise 时调用兼容下载方式下载。

为了保证兼容下载方式执行成功,需要在构造函数中设置相关参数!详见 构造函数 参数说明

如果自己的业务代码中已经有了如 环境检测、异常捕获等逻辑。可以直接调用 saverCompatible() 方法下载文件。

saverCompatible()

Arguments

Returns

Example

import DL from 'js-file-saver';

const downloadBook = new DL({
  action: '/api/downloadBook', 
  method: 'post', 
  payload: {
    bookId: 1,
  },
});

// 兼容下载无任务事件可触发,如需要可添加相关用户提示,缓解焦虑。如:'下载中!请稍后...'
downloadBook.saverCompatible();