检测文件是否在ASP.NET MVC 5中成功下载 [英] Detect if file was downloaded successfully in ASP.NET MVC 5

查看:39
本文介绍了检测文件是否在ASP.NET MVC 5中成功下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 ActionResult HttpResponseMessage result.Content 中提供了 StreamContent 文件.现在,我想跟踪下载状态,下载后立即删除文件.

my ActionResult provides a StreamContent file in the HttpResponseMessage result.Content. Now I would like to track the state of the download, to delete the file immediately after it was downloaded.

我找到了使用 ByteStream 的解决方案,该解决方案将文件拆分为多个块,但是在某些授权测试拒绝该代码的情况下,它无法提供 HttpStatusCode 和其他信息.请求.

I found solutions that use the ByteStream, which split the file into chunks but that lacks the possibilities to provide a HttpStatusCode and other information in case some authorization tests deny the request.

这是我当前的控制器:

    [HttpGet]
    public HttpResponseMessage GetZipDownload(string token, string guid)
    {

        if (!dOps.ValidateToken(token))
        {
            return Request.CreateResponse(HttpStatusCode.Unauthorized,
                new HttpError("Unauthorized"));
        }


        Device device = dOps.GetDeviceFromToken(auth);
        AssetOperations assetOps = new AssetOperations();

        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);

        FileStream file = assetOps.GetZip(guid);
        var content = new StreamContent(file);
        result.Content = content;
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
        result.Content.Headers.Add("Connection", "Keep-Alive");
        result.Content.Headers.Add("Content-Length", (file.Length).ToString());

        return result;
    }

在我进一步研究 ByteStream 解决方案之前,我想问一下是否有人知道ASP.NET MVC 5的可靠解决方案.

Before I dig deeper into the ByteStream solution, I would like to ask if anybody probably knows about a reliable solution for ASP.NET MVC 5.

推荐答案

您可以注册EndRequest事件处理程序来实现它.

You can register EndRequest event handler to achieve it.

  1. 覆盖MvcApplication类中的 Init()方法,并注册到 EndRequest 事件处理程序.
  2. 使用恒定键将文件路径保存到 HttpContext.Current.Items 中.
  1. Override Init() method inside MvcApplication class, and register to EndRequest event handler.
  2. Save into HttpContext.Current.Items the file path with a constant key.

Global.asax文件

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        // some startup code, removed for clearity
    }

    public override void Init()
    {
        base.Init();
        EndRequest += MvcApplication_EndRequest;
    }

    private void MvcApplication_EndRequest(object sender, EventArgs e)
    {
        if (
            HttpContext.Current.Request.Url.AbsolutePath == "/DownloadFileEndedWebsite/Home/Contact" // This should be adjusted.
            && HttpContext.Current.Items.Contains("DownloadFilePath"))
        {
            var filePath = HttpContext.Current.Items["DownloadFilePath"];
            // Delete file here..
        }
    }
}

HomeController.cs

public ActionResult Contact()
{
    HttpContext.Items.Add("DownloadFilePath", "DownloadFilePathValue");
    return View();
}

这篇关于检测文件是否在ASP.NET MVC 5中成功下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆