I have to know file downloaded status in clientside and I am using ASP.NET MVC to do the task. I cannot use cookies to know the status as the page is opening in iframe in some other application which doesn't allow me to keep cookies.
I would like to access the QueryString from RedirectToAction Url which is " DownloadUrl ". My screenshot to get the value and respected view and controler code is as below. How can I achieve the "downloadStatus" querystring in my case? Please help.
Index.cshtml
@{
ViewBag.Title = "Home Page";
}
<script type="text/javascript">
$(document).ready(function () {
$("#download").on("click", function () {
$('#statusDiv').show();
setTimeout(checkDownloadStatus, 1000); //Initiate the loop to check the downloadStarted value.
});
var checkDownloadStatus = function () {
// Get querystring value for downloadStarted
if (getUrlParameter("downloadStatus") == 1) {
$('#statusDiv').hide();
} else {
downloadTimeout = setTimeout(checkDownloadStatus, 1000); //Re-run this function in 1 second.
}
};
// get querystring value
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = window.location.search.substring(1),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
}
}
};
});
</script>
@using (Html.BeginForm(null, null, FormMethod.Post, new { @id = "formDownload" }))
{
<div class="row">
<div id="statusDiv" style="display:none;">File is downloading ...</div>
<button type="submit" formaction="~/Home/FileDownload" id="download" value="download" formmethod="post">
Download
</button>
</div>
}
HomeController.cs
using System.Threading.Tasks;
using System.Web.Mvc;
namespace RedirectToActionDemo.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public async Task<ActionResult> FileDownload()
{
var fileStream = System.IO.File.ReadAllBytes(@"D:\Documents\Information.docx");
DownloadFileResult res = new DownloadFileResult();
res.FileResult = fileStream;
res.MimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
res.FileName = "Information.docx";
TempData["Result"] = res;
return RedirectToAction("downloadUrl", new { downloadStatus = "1" });
}
public ActionResult DownloadUrl(string downloadStatus)
{
DownloadFileResult res = TempData["Result"] as DownloadFileResult;
return File(res.FileResult, res.MimeType, res.FileName.Replace("\"", ""));
}
}
public class DownloadFileResult
{
public byte[] FileResult { get; set; }
public string FileName { get; set; }
public string MimeType { get; set; }
}
}
This should give you the last part of your url:
@{
var id = Request.Url.Segments.Last();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.