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

170
Visualizações
Return file to download using WebAPI HTTPGet to View(User)

I am new to the Javascript side of things and I am trying to work out how to return a file to a user to download from a ApiController

ApiController:

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

        string url = await GetURL("id00032912");
        //this is hard coded until I properly work out how to get a value passed along from HttpGet
        HttpResponseMessage result = null;

        WebClient myClient = new WebClient();

        byte[] bytes = myClient.DownloadData(url);

        result = Request.CreateResponse(HttpStatusCode.OK);
        result.Content = new ByteArrayContent(bytes);
        result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
        result.Content.Headers.ContentDisposition.FileName = "test" + ".txt";

        return result;

    }
}

Now the result is filled with the required data but I cannot manage to get it to return.

I use this call in JS to call and get to the function

await WebApi.getJSON('Tick', 'Download', ID);

This is the WebApi call expanded

static getJSON(controller: string, action: string, querystring: string, data?: any): any {
        return this.get("api", controller, action, querystring, data);
    }

    static getODataJSON(controller: string, querystring: string, data?: any): any {
        return this.get("odata", controller, null, querystring, data);
    }

My Issue is Im not sure how to properly handle the value being returned in Javascript and how to force it to download on the users side. I was wondering if anyone could show some examples or point me in the right direction?

Any help would be appreciated thank you!.

NOTE I am using ApiController so I cannot use return File() like a lot of other similar questions use.

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