Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

167
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda