'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.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source