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('Incident', '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.