问题
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