Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

246
Visualizações
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 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda