我正在使用角度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
}
}
答案 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