I have to actions in a controller:
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
[HttpGet("redirect1")]
public IActionResult Redirect1()
{
var r = RedirectToAction(nameof(GetStream));
return r;
}
[HttpGet("stream")]
public IActionResult GetStream()
{
var ms = new MemoryStream(Encoding.UTF8.GetBytes("Hello Word"));
return File(ms, "application/octet-stream", "test.xyz", true);
}
}
When I typed https://localhost:44352/api/values/redirect1, the save as dialog is open for download but the url in browser is not changed to https://localhost:44352/api/values/stream. Is a way to change also the url. In case the GetStream action return a json the url is changed to https://localhost:44352/api/values/stream. How is possible also to change the url in case I return FileStreamResult.
Is possible to have something in the view like "waiting..." and return FileStreamResult
You can return a HTML to make it:
public IActionResult Redirect1() {var r = RedirectToAction(nameof(GetStream));return r;var url = this.Url.Action(nameof(GetStream)); return Content( $"<script>history.pushState({{}},'downloading','{url}');history.go()</script>", "text/html" ); }The key is spelling the magic from JavaScript so that you can change the browser behavior as you like.
Again, you can do that by a HTML Content. Lets' say you want to display a
waiting...message and trigger the downloading in 1 second:What if your javascript goes more complicated? Simply create a
cshtmlView and put the content within a View file.