• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

185
Vistas
Downloading File Object from C# API Controller to Client using Javascript

I am trying to return a file for a user to download when they click a download button on the page. The trick is I am downloading the file from an internal URL service. I have it set up as follows

The Download controller a function I call to return the File()

    public class DownloadController : ControllerBase
    {
        public FileResult DownloadFile(string url)
        {
            WebClient internalDownload = new WebClient();
            byte[] bytes = internalDownload.DownloadData(url);

            return File(bytes, "application/octet-stream", "test" + ".txt");
        }
    }

The API Controller this is what the JS calls

    public class TickController : ApiController
    {
        [Route("api/Tick/Download")]
        [HttpPost]
        public async Task<IActionResult> Download([FromBody] string ID)
        {

            string url = await GetFile("D00023974");
            DownloadController downloader = new DownloadController();

            return downloader.DownloadFile(url);

            

        }
    }

The Javascript I have for testing purposes at the moment

The API Manager Javascript which sends the request to the above Controller:

export class ApiCall {
    static post(controller: string, action: string, data: any, qString?: string) {
        var url = '/api/' + controller;
        if (action != null)
            url += '/' + action;
        if (querystring != null)
            url += "?" + qString;
        return $.ajax({
            url: url,
            data:JSON.stringify(data),
            method: 'POST',
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
        });
    }
}

And the current test call and checking if its returning anything

async downloadAttach(ID: any) {
        window.open(await WebApiManager.post('Tick', 'Download', ID));
}

The above returns an object as follows:

[[Prototype]]     Object
ContentType       application/octet-stream
FileContents      //Filled with Base64
FileDownloadName  test.txt
_proto_           Object

However I'm not sure how to initiate the download on the client side of the returned file. Any help would be greatly appreciated.

about 3 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda