Emili Bellot Pulido
Emili Bellot Pulido

Reputation: 31

Send document to client server from .net server

I have an API that offers a file translation service. I have been able to generate a new the new translated file but I have not been able to get it to the client correctly, because the document downloaded it is not readable.

This is my controller code:

[HttpPost]
[Route("TranslateDocument/{inputLanguage}/{outputLanguage}")]
[ProducesResponseType(typeof(FileContentResult), StatusCodes.Status200OK)]
public async Task<ActionResult<FileInfo>> TranslateDocument(string inputLanguage, string outputLanguage, [FromForm] IFormFile file)
{
    try
    {
        FileContentResult result = await _deeplService.TranslateFile(inputLanguage, outputLanguage, file);
        return Ok(result);
    }
    catch (Exception ex)
    {
        return ManageExceptions(ex);
    }
}

In my service I have the following function:

public async Task<FileContentResult> TranslateFile(string inputLang, string outputLang, IFormFile file){
    //Code to translate the file into outputLang
    ...

    //Get bytes from Document generated
    byte[] bytes = System.IO.File.ReadAllBytes("localpath from doc");

    //Send the File to Download.
    return new FileContentResult(bytes, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
}

And this is the response I am getting on the client site:

{
    "fileContents": "UEsDBBQAAA...AAyCUAAAAA",
    "contentType": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    "fileDownloadName": "",
    "lastModified": null,
    "entityTag": null,
    "enableRangeProcessing": false
}

And when I do the const url = window.URL.createObjectURL(new Blob([response.fileContents], {type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}));, the file downloaded is not readable in word

Upvotes: 1

Views: 38

Answers (0)

Related Questions