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

How to change the file name during the download process in Jhipster (java + angular)

这是从控制器打开它的代码:

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

并且在 HTML 中,这是在从服务返回的 object 中传递数据时使用的,如下所示:

 <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 相关)。我希望它可以让用户轻松找到下载的文件。我尝试添加 header content-filename。但我无法解析它并设置它。看起来 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>

然后您可以在控制器中输入任何您想要的文件名:

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

无论如何,设置文件附件名称的正确 header 应该是:

Content-Disposition: attachment; filename=custom_name.pdf;

我没有测试这段代码。