Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ngx-filesaver

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ngx-filesaver

Simple file save with FileSaver.js

  • 2.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
52K
increased by7.82%
Maintainers
1
Weekly downloads
 
Created
Source

ngx-filesaver

Simple file save with FileSaver.js

NPM version Build Status

示例

安装

npm install file-saver ngx-filesaver --save

添加 FileSaverModule 模块到项目中:

import { FileSaverModule } from 'ngx-filesaver';
@NgModule({
  imports: [ FileSaverModule ]
})

使用方法

支持服务 FileSaverService.save() 或属性指令 fileSaver 两种保存方式。

1、FileSaverService

constructor(private _http: Http, private _FileSaverService: FileSaverService) {
}

onSave() {
  let options = new RequestOptions({
    responseType: ResponseContentType.Blob // 这里必须是Blob类型
  });

  this._http.get('demo.pdf', options).subscribe(res => {
    this._FileSaverService.save((<any>res)._body, fileName);
  });
}

2、fileSaver 属性指令

配置型
<button type="button"
        fileSaver
        [method]="'GET'"
        [fileName]="'中文pdf.pdf'"
        [url]="'assets/files/demo.pdf'"
        [header]="{ token: 'demo' }"
        [query]="{ pi: 1, name: 'demo' }"
        (success)="onSuc($event)"
        (error)="onErr($event)">Download PDF</button>

fileSaver:属性指令名称。 参数说明

参数说明类型默认值
method请求方法类型stringGET
url下路路径string-
fileName文件名string-
query额外的查询参数,等同 paramsstring-
header请求的 headers 属性值,一般用来指定 token 之类any-
success下载成功回调EventEmitter<any>-
error下载错误回调EventEmitter<any>-
自定义Http型
<button type="button"
        fileSaver
        [http]="onRemote('pdf', true)">Download PDF</button>
onRemote(type: string, fromRemote: boolean): Observable<Response> {
  let options = new RequestOptions({
    responseType: ResponseContentType.Blob
  });
  return this._http.get(`assets/files/demo.${type}`, options).map(response => {
    response.headers.set('filename', `demo.${type}`)
    return response;
  });
}
关于文件名

文件名的获取按以下优先级:fileName =》 response.headers.get('filename') =》 response.headers.get('x-filename')。

如果你请求的是一个CORS跨域地址,需要注意设置 Access-Control-Allow-Headers: filename,以免无法获取。

Keywords

FAQs

Package last updated on 08 May 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc