javascript - 如何在使用 FileContentResult ActionResult 时了解客户端 ASP.NET MVC 中的文件下载状态?
问题描述
我必须知道客户端的文件下载状态,并且我正在使用 ASP.NET MVC 来完成任务。我无法使用 cookie 来了解状态,因为页面在 iframe 中在其他一些不允许我保留 cookie 的应用程序中打开。
我想从“ DownloadUrl ”的 RedirectToAction Url 访问 QueryString。我获取价值和尊重的视图和控制器代码的屏幕截图如下。在我的情况下,如何实现“downloadStatus”查询字符串?请帮忙。
索引.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>
}
家庭控制器.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; }
}
}
解决方案
推荐阅读
- c - 我的代码中是否有任何未初始化的值?
- javascript - 使用按位运算符时,javascript 和 python 的结果不同
- java - 无法让 DrJava 运行
- loops - VLC:使用多个视频 PID 循环播放 MPEG-TS 文件
- ios - 在表格视图中每 5 个单元格后添加一个广告正在替换内容
- python-3.x - 在 ubuntu 中为 python3.7.0 创建 virtualenv 不起作用
- dreamweaver - $ 未在导入的 js 中定义
- python-3.x - 运行 tmp() 的结果是什么以及为什么?
- c# - 如何 Console.Writeline IEnumerable<(int a, int b, int c)>?
- r - 在R中按时间计数