简体   繁体   English

通过“Blob”下载文件时以角度更改文件名

[英]change file name in angular when download that by "Blob"

i want to download file from server in angular :我想以 angular 从服务器下载文件:

this code from service:来自服务的此代码:

  DownloadVerifyFile(requestId, fileId): any {
    return this.httpClient
        .get(this.appConfig.apiEndpoint + '/VerifyRequest/File/' + requestId + '/' + fileId,
            { responseType: 'blob' });
}

and this code for download that file in brwoser:以及在 brwoser 中下载该文件的代码:

 DownloadFile(fileId): void {
    this.requestService.DownloadVerifyFile(this.id,fileId).subscribe(response => {
      const a = document.createElement('a')
      const objectUrl = URL.createObjectURL(response)
      a.href = objectUrl
      a.download = response;
      a.click();
      URL.revokeObjectURL(objectUrl);
    });
  }

but i have a problem with that , when i downlaod file , file name is this [object Blob] but i want to download by orginal name for example if file is file1.jpg , when downloaded file name must be fil1.jpg not [object Blob] .但是我有一个问题,当我下载文件时,文件名是这个[object Blob]但我想按原始名称下载例如如果文件是file1.jpg时下载的文件名必须是fil1.jpg而不是[object Blob] how can i solve this problem ???我怎么解决这个问题 ???

Because you have named the file by response(It is an object).因为您已通过响应命名文件(它是一个对象)。 You were almost achieved.你几乎达到了。 Just a little change as following:只是一点点变化如下:

a.download = response;

to

a.download = 'fil1.jpg';

Then you will get the correct file name.然后你会得到正确的文件名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM