【问题标题】:ASP.NET MVC : capture result of download promptASP.NET MVC:捕获下载提示的结果
【发布时间】:2019-01-14 15:46:48
【问题描述】:

我有一些代码可以使文件可用并提示用户open/download/cancel如果文件被打开或下载,我想采取行动,但如果取消则不采取行动

这是下载线,来自控制器:

return File(fileAsBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);

我想我的问题是,如何从该操作中获取返回值?我会在控制器或调用它的视图中看到结果吗?

我是一个苦苦挣扎的新手。不要假设基础知识。

谢谢。

【问题讨论】:

  • 对不起,这是不可能的,响应被发送到客户端,客户端不必回复说它做了什么......您可能可以使用带有 AJAX 请求的 Javascript捕获结果,但这取决于浏览器并且没有通用的解决方案
  • 谢谢 marc_s。我会以此作为答案。目前,我认为我不想为此进入客户端代码。我只是“假设”他们拿走了文件。

标签: asp.net-mvc download actionlink actionresult html.actionlink


【解决方案1】:

你的控制器:

public class HomeController : Controller
{
    [HttpPost]
    public ActionResult Upload(string SubmitButton, HttpPostedFileBase file)
    {
        if (SubmitButton == "Cancel")
        {
            //redirect to a cancel page
            //return RedirectToAction("Index", "Home");
        }

        //put a breakpoint in this metho to interogate file, 
        //the file you are going to save to db or do something with

        return View();
    }

    public ActionResult Tut115()
    {
        return View();
    }

你的看法:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Tut115</title>
</head>
<body>
    <div>
        @using (Html.BeginForm("Upload",
        "Home",
        FormMethod.Post,
        new { enctype = "multipart/form-data" }))
        {
            <input type="file" name="file" id="file" /><br>
            <input type="submit" name="SubmitButton" value="Cancel" />
            <input type="submit" name="SubmitButton" value="Upload" />
        }
    </div>
</body>
</html>

【讨论】:

  • 感谢 kblau。我正在为客户端下载提供文件,而不是上传。当客户在浏览器中选择“打开/保存/取消”时,尝试捕获客户的操作...
  • 您可以研究下载附件。我的下载附件代码以此结尾: Response.Buffer = true;响应。清除(); Response.AddHeader("content-disposition", "attachment; filename=" + myFileName + ".csv"); Response.ContentType = "文本/csv"; Response.BinaryWrite(字节); Response.End();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-20
  • 2013-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多