1

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.

C69-001
  • 31
  • 3
  • I feel like you've misunderstood a couple things, i.e. you shouldn't be creating a new controller from within another controller and you shouldn't be downloading a file from a POST. It is possible to continue the way you've done it by downloading the base 64 as a file with the correct MIME type by following something like [this](https://stackoverflow.com/a/57804808/3688864) – Shoejep Feb 20 '22 at 14:05
  • @Shoejep the only problem is because Im using an `ApiController` Controller for my main class I cannot use File() as its only part of `ControllerBase`, so I was trying to work around it as no one else could help me with the other solution I tried. Unless there is an easier way around it. – C69-001 Feb 20 '22 at 14:15
  • You should be able to return a new FileContentResult from your ApiController – Shoejep Feb 20 '22 at 14:59
  • @Shoejep I had tried 'FileContentResult' in the past however it throws the same error as 'File()' cant be used as a method. Which ended up being because its only available in ControllerBase and not ApiController. – C69-001 Feb 20 '22 at 15:03
  • What version of ASP.NET or ASP.NET Core are you using? – Shoejep Feb 20 '22 at 15:06

0 Answers0