首页 > 解决方案 > 如何使用 Angular 7 将存储在 SQL Server 数据库中的文件作为 varbinary 类型下载?

问题描述

我正在尝试使用 typscipt angula 7 将存储在 sql server 数据库表中的文件作为 vrabinary 类型下载

我能够从数据库中获取具有 fileName 和 fileContent 作为 varbinay 类型的对象(r​​es)

 public download = (fileId) => {
   
      this.fileService.getFilediaById(fileId)
      .subscribe(res => {
        this.isLoading = false;
        if (res != null)
        {
          
          const data = res.fileContent; // of type varbinary
          const filename = res.fileName;
          const blob = new Blob([data], { type: 'application/pdf' });
          this.fileUrl = this.sanitizer.bypassSecurityTrustResourceUrl(window.URL.createObjectURL(blob));
        }

      },
        error => {
          console.log(error);
        });
  }
<a [href]="fileUrl" [download]="fileName" mat-menu-item (click)="download(fileId)">Download</a>

但文件为空并出现错误“失败 - 无文件”

谢谢

标签: c#sql-serverangulartypescriptdownload

解决方案


推荐阅读