Giter Site home page Giter Site logo

jkhhuse / ngx-filesaver Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cipchk/ngx-filesaver

0.0 1.0 0.0 895 KB

Simple file save with FileSaver.js

Home Page: https://cipchk.github.io/ngx-filesaver/

License: MIT License

JavaScript 25.43% TypeScript 74.57%

ngx-filesaver's Introduction

ngx-filesaver

Simple file save with FileSaver.js

NPM version Build Status

示例

demo

安装

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 
        [fileName]="'中文pdf.pdf'"
        [url]="'assets/files/demo.pdf'"
        [header]="{ token: 'demo' }"
        [query]="{ pi: 1, name: 'demo' }">Download PDF</button>

fileSaver:属性指令名称。

url:下路路径。

fileName:文件名。【选填】

header:请求的 headers 属性值,一般用来指定 token 之类。【选填】

query:额外的查询参数。【选填】

自定义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,以免无法获取。

ngx-filesaver's People

Contributors

mo-gong avatar angular-cli avatar cipchk avatar

Watchers

秋天 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.