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.1.1
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

ngx-filesaver

Simple file save with FileSaver.js

NPM version Build Status

中文版

Examples

Installation

npm install file-saver ngx-filesaver --save

Add the FileSaverModule module to your project:

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

Instructions

There are two ways to save a file: using FileSaverService.save() or using the fileSaver directive.

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 directive

Configuration example
<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: the directive name Parameters

ParameterDescriptionTypeDefault
methodRequest method typestringGET
urlRequest URLstring-
fileNameFilename when downloadingstring-
queryAdditional query parameters. Equivalent to params valuestring-
headerHeader configuration. Usually used for especifying access tokensany-
successDownload success callbackEventEmitter<any>-
errorDownload error callbackEventEmitter<any>-
Custom HTTP type
<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;
  });
}
About filenames

The name for the downloaded file is obtained with the following priority:

  1. fileName
  2. response.headers.get('filename')
  3. response.headers.get('x-filename')。

If you are requesting a CORS address, you need to pay attention to the request headers. Setting Access-Control-Allow-Headers: filename should be sufficient

Keywords

FAQs

Package last updated on 20 Sep 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