What is the best way to return a file or ErrorMessage from an Asp.net-mvc controller action?

I have the following javascript code and controller action in my asp.net-mvc project:

JavaScript:

$("#exportPPT").live('click', function (e) { window.location.href = "/Initiative/GenerateFile" + GenerateParams(); }); 

C # controller:

  public ActionResult GenerateFile(MyParams myParams) { var template = Server.MapPath(PPT_ROOT + "/template.pptx"); IEnumerable<Order> orders = Model.GetOrders(myparams); var pptResults = GeneratePowerpointFile(orders); return File(pptResults.Content, "application/vnd.ms-powerpoint", pptResults.FileName); } 

but under certain conditions, say when orders.Count () is 0, instead of generating a file, I would prefer to return an error message to the user, indicating that you have an error.

What is the best way to achieve this, given the code above? I was thinking of changing it to an ajax call, but I was not sure how to load my Fie () and the package that is in the json request (or if this is supported).

Any suggestions?

+7
jquery asp.net-mvc powerpoint
source share
6 answers

I would initiate a $ .get request to another controller action that checks the number of orders. Return this value with the error message if necessary. Display an error message when necessary, otherwise handle the redirect to download the file. This is an additional controller call, but it allows you to fully control and handle the possibility of error without redirecting your user.

 $("#exportPPT").live('click', function (e) { $.get( "/Initiative/CheckForOrders" + GenerateParams(), function( data ) { if (data.IsValid) { window.location.href = "/Initiative/GenerateFile" + GenerateParams(); } else { alert(data.ErrorMessage); // or show a div containing error message } }); }); 

Controller action:

 public ActionResult CheckForOrders(MyParams myParams) { IEnumerable<Order> orders = Model.GetOrders(myparams); if (orders.Any()) return Json(new { IsValid=true }, JsonRequestBehavior.AllowGet); return Json(new { IsValid=false, ErrorMessage="No orders" }, JsonRequestBehavior.AllowGet); } 
+8
source share

Would I return a status representing that the resource does not exist and returns null? Then you can process it accordingly in javascript, without worrying that several ajax calls to check one are available or there are security implications if someone bypasses these checks.

For example...

controller

 public ActionResult GenerateFile(MyParams myParams) { var template = Server.MapPath(PPT_ROOT + "/template.pptx"); IEnumerable<Order> orders = Model.GetOrders(myparams); if(!orders.Any()){ Response.StatusCode = (int)HttpStatusCode.NotFound Response.StatusDescription = HttpStatusCode.NotFound.ToString(); Response.TrySkipIisCustomErrors = true; return EmptyResult; } var pptResults = GeneratePowerpointFile(orders); return new File(pptResults.Content, "application/vnd.ms-powerpoint", pptResults.FileName); } 
+4
source share
 if (count==0) return View(); else return File(...) 

Can't it work?

0
source share

I would suggest redirecting the user if order.Count () is 0 or some other error. Something like that.

 public ActionResult GenerateFile(MyParams myParams) { var template = Server.MapPath(PPT_ROOT + "/template.pptx"); IEnumerable<Order> orders = Model.GetOrders(myparams); if(orders.Count() == 0){ return RedirectToAction("Orders","ordersError",new { ID = "Error message"}); }else{ var pptResults = GeneratePowerpointFile(orders); return File(pptResults.Content, "application/vnd.mspowerpoint",pptResults.FileName); } } 

So, you create an informative OrderError view that displays your error message.

0
source share

If you have to handle even internal server errors, you will need to configure your own filter, apply it to your controller method, which intercepts errors related to the disk or any, and processes it gracefully, and returns your view to some significant data message.

please refer to this post about creating an asp.net mvc filter and its associated javascript code.

ASP.NET MVC Ajax Error Handling

0
source share

Maybe this post may be useful: Upload a file using Javascript / jQuery

I believe that the JQuery File Download plugin deserves some attention, it handles errors correctly :) See also your online demo ...

0
source share

All Articles