downloaded docx file has unreadable content

Discussão em 'Angular' iniciado por Joost V., Abril 7, 2025.

  1. Joost V.

    Joost V. Guest

    When I download a docx document from the server i get "Word has found unreadable content in xxxx.docx" error. I figured that a docx file is just xml, so i could use a string. Using a uint8array gives the same error.

    This is the server side controller code:


    [HttpPost("releasenotes")]
    public async Task<ActionResult<ReleaseNotes>> getInfraModelReleaseNotesAsync(int id)
    {
    ReleaseNotes releaseNotes = await Task.Run(() =>
    {
    ReleaseNotes rn = new ReleaseNotes();
    FileStream fs = infraModelService.getInfraModelReleaseNotes(id);
    string path = fs.Name;
    rn.name = Path.GetFileName(path);
    using (MemoryStream ms = new MemoryStream())
    {
    fs.CopyTo(ms);
    ms.Seek(0, SeekOrigin.Begin);
    rn.data = Encoding.UTF8.GetString(ms.ToArray());
    }
    return rn;
    });
    return releaseNotes;
    }

    and here is my angular typescript code: infraModel.service.ts:


    getInfraModelReleaseNotes(id: number): Observable<ReleaseNotes> {
    this.loggerService.debug('InfraModel release notes bestand wordt gedownload, ialaag: ' + id);
    return this.http.post<ReleaseNotes>(this.infraControllerUri + "releasenotes?id=" + id, null).pipe();
    }

    infraModel.component.ts:


    public infoButtonClick(id: number): void {
    this.loggerService.debug('info opvragen met id: ' + id);
    this.infraModelService.getInfraModelReleaseNotes(id).subscribe(
    (response) => {
    const data: any = response.data.trim();
    const BOM = new Uint8Array([0xEF, 0xBB, 0xBF]);
    var blob = new Blob([BOM, data], { type: 'text/xml;charset=utf-8' });
    saveAs(blob, response.name);
    },
    (err: HttpErrorResponse) => { this.alertService.error(`Fout bij ophalen inframodel release notes: ${this.alertService.parseHttpError(err)}`); }
    );
    }

    and releaseNotes:


    export class ReleaseNotes {
    public name: string;
    public data: string;
    }

    I cannot figure out what i am doing wrong. Can somebody give me a hint please?

    Continue reading...

Compartilhe esta Página