angular - Jhipster下载过程中如何更改文件名(java + angular)

标签 angular typescript jhipster

这是似乎从 Controller 打开它的代码:

 openFile(contentType, field) {
    return this.dataUtils.openFile(contentType, field);
  }


在 HTML 中,这用于从服务返回的对象中传递数据,如下所示:

 <button type="submit" (click)="openFile(dataCleansing.fileContentType, dataCleansing.uploadedFileContent)"
                class=" btn
                btn-info viewTheme">
                <fa-icon [icon]="'download'"></fa-icon>
                </fa-icon>&nbsp;<span> Open/View File</span>
            </button>


我的目标是下载文件,下载后它带有文件名,而不是 download(1)。或 download(2)在 Windows 中,它使用一些随机 ID(与 fileID 无关)。我想要它,以便用户可以轻松找到下载的文件。我尝试添加标题内容文件名。但我无法解析并设置它。看起来 Jhipsters 内置了名为 data-utils.service.ts 的服务控制下载文件功能。在节点模块中看起来像这样:

import { ElementRef } from '@angular/core';
/**
 * An utility service for data.
 */
export declare class JhiDataUtils {
    constructor();
    /**
     * Method to abbreviate the text given
     */
    abbreviate(text: string, append?: string): string;
    /**
     * Method to find the byte size of the string provides
     */
    byteSize(base64String: string): string;
    /**
     * Method to open file
     */
    openFile(contentType: string, data: string): void;
    /**
     * Method to convert the file to base64
     */
    toBase64(file: File, cb: Function): void;
    /**
     * Method to clear the input
     */
    clearInputImage(entity: any, elementRef: ElementRef, field: string, fieldContentType: string, idInput: string): void;
    /**
     * Sets the base 64 data & file type of the 1st file on the event (event.target.files[0]) in the passed entity object
     * and returns a promise.
     *
     * @param event the object containing the file (at event.target.files[0])
     * @param entity the object to set the file's 'base 64 data' and 'file type' on
     * @param field the field name to set the file's 'base 64 data' on
     * @param isImage boolean representing if the file represented by the event is an image
     * @returns a promise that resolves to the modified entity if operation is successful, otherwise rejects with an error message
     */
    setFileData(event: any, entity: any, field: string, isImage: boolean): Promise<any>;
    /**
     * Method to download file
     */
    downloadFile(contentType: string, data: string, fileName: string): void;
    private endsWith;
    private paddingSize;
    private size;
    private formatAsBytes;
}


任何方向和提示将不胜感激!

最佳答案

我猜你真正想要的是下载文件而不是打开它们。 JhiDataUtils有一个专门为此的方法:

downloadFile(contentType: string, data: string, fileName: string): void;

尝试使用该方法而不是 openFile() ,在 HTML 中是这样的:

<button type="submit" class="btn btn-info viewTheme" 
    (click)="downloadFile(dataCleansing.fileContentType, dataCleansing.uploadedFileContent)">
    <fa-icon [icon]="'download'"></fa-icon> Download File
</button>

然后,您可以将所需的任何文件名放入 Controller 中:

  downloadFile(contentType: string, field: string) {
    const filename = 'report_' + moment().format('YYYY-MM-DD-HH-mm');
    this.dataUtils.downloadFile(contentType, field, filename);
  }

在任何情况下,设置文件附件名称的正确标题应该是:
Content-Disposition: attachment; filename=custom_name.pdf;

我没有测试这段代码。

关于angular - Jhipster下载过程中如何更改文件名(java + angular),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59910524/

相关文章:

java - JHipster:错误:Image和ImageType的关系中,ImageType没有声明

angular - 如何将数值变量传递给 Angular 中的正则表达式

angular - 如何从 firebase.firestore.DocumentSnapshot 获取文档数据?

JavaScript/TypeScript - 对象仅分配可用属性

typescript - 将函数结果绑定(bind)到 Angular2 组件 (TypeScript)

javascript - 如何为 Angular Material Slide Toggle 设置默认值?

jhipster - 在 jhipster 中配置电子邮件

angular - 具有 Primeng 动态对话框的多个服务相互干扰

Angular 5 - 我们如何测试 router.navigate([id],{relativeTo : this. _activatedRoute}])

jhipster - 分别在 JHipster 和 Spring boot plus angular 之间进行选择