PDF下载,角度5

时间:2018-09-01 14:11:09

标签: angular base64 angular5

我正在使用角度5.如何从base64字符串制作下载链接或pdf查看器。

我尝试过

<input name="file2" type="file" [(ngModel)]="myfile" base-sixty-four-input >

在TypeScript类中

export class AppComponent {
  title = 'test2';
  myfile = {
    filetype: "application/pdf",
    filename: "test.pdf",
    base64: 'JVBERi0xLjUNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFuZyhlbi1HQikgL1N0cnVjdFRyZWVSb290IDEwIDAgUi9NYXJrSW5mbzw8L01hcmtlZCB0cnVlPj4
  }
}

2 个答案:

答案 0 :(得分:1)

尝试添加链接,例如

<a [href]="downloadLink">Download {{ myfile.filename }}</a>

在您的课堂上,

//First import
import { DomSanitizer } from '@angular/platform-browser';

// Then
constructor(private sanitizer: DomSanitizer) {}

myfile = {
    filetype: "octet-stream;base64",
    filename: "test.pdf",
    base64:  'JVBERi0xLjUNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFuZyhlbi1HQikgL1N0cnVjdFRyZWVSb290IDEwIDAgUi9NYXJrSW5mbzw8L01hcmtlZCB0cnVlPj4'
};

downloadLink = this.sanitizer.bypassSecurityTrustUrl(`data:${this.myfile.filetype},${this.myfile.base64}`);

答案 1 :(得分:0)

在app.component中添加以下文件类型

filetype: application/octet-stream;base64