首页 > 解决方案 > 如何在使用 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; }
    }
}

标签: javascriptjqueryasp.net-mvc

解决方案


这应该给你你的网址的最后一部分:

@{
   var id = Request.Url.Segments.Last();
}

关于上述的一些信息


推荐阅读