Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!