I have an ASP.NET MVC app. In my view, I have a button that says "Download". When a user clicks this button, the text changes to "Downloading...". My code for this looks like this:
View.cshtml
<button id="myButton" onclick="downloadClick();"><span>Download</span></button>
function downloadClick() {
$('#myButton').html('<span>Downloading...</span>');
window.location = '/download/latest';
}
This code successfully downloads a file. The controller action located at /download/latest
looks like the following:
MyController.cs
public class MyController {
public ActionResult DownloadLatest() {
var someUrl = "https://my-domain.com/files/latest";
using (var fileClient = new HttpClient())
{
var file = await fileClient.GetStreamAsync(fileUrl);
Response.ClearContent();
Response.ClearHeaders();
Response.AppendHeader("Content-Disposition", "Attachment; Filename=latest.txt");
return new FileStreamResult(file, "txt");
}
}
}
The file successfully downloads. However, I can't figure out how to restore the button text to just "download" after the response is retrieved. How do I do that?
Thank you!