ASP.NET MVC : capture result of download prompt

谁说胖子不能爱 提交于 2020-01-05 04:23:15

问题


I have a bit of code that makes a file available and prompts user to open/download/cancel. I would like to take an action if the file is opened or downloaded, but not if canceled.

This is the download line, from the controller :

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

I guess my question is, how do I capture a return value from this action? Would I see the result in the controller or in the view that calls it?

I'm a struggling newbie. Don't assume foundational knowledge.

Thanks.


回答1:


Your controller:

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();
    }

Your 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>


来源:https://stackoverflow.com/questions/51728706/asp-net-mvc-capture-result-of-download-prompt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!