0

I have a controller method which returns response to download. It is working fine but when there is an exception occurs a blank page returned on the current page, where download is requested.

Also I am sending some form data to request the download.

Instead I need to show the error message as popup. How could I do that?

MVC Controller Method to download pdf. Its return pdf to download but sometimes when exception occurs it return a blank page.

  [HttpPost]
  public void Downloadpdf()
  {
      try
      {
        // Generate pdf
            Response.Clear();
            Response.AddHeader("Content-Type", "application/pdf");
            Response.AddHeader("Content-Disposition", "attachment;filename=Newpdf.pdf");
            byte[] pdf = ms.ToArray();
            Response.OutputStream.Write(pdf, 0, pdf.Length);
            Response.End();

       }
       catch (Exception ex)
       {
           Response.End();
       }
   }  

How should I show popup for the exception. And avoid blank page. Also it is a Single Page Application.

2
  • You can redirect to another controller by passing exception object as model from catch block. And from that controller you can render respective view notifying user about the error. Commented Jan 6, 2014 at 5:10
  • Actually i need to show the error in a popup window in the same page where i request(submit form) for download. @Rumit Parakhiya Commented Jan 6, 2014 at 5:31

2 Answers 2

1

1) Remove void and use ActionResult

2) Use "return View("YourViewName");" after catch statement

3) In catch, add a line: ViewBag.Exception = ex;

4) On the view where you return, type

@if(ViewBag.Exception == null)
{
}
else
{
@ViewBag.Exception
}

5) You want to show on popup, use alert on the same view using javascript.

Hope the points satisfy your problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Its a single Page Application where routing is done in router.js(#tag routing), so i could not able to return a view.
0

in you main page,put a iframe 0 width 0 height
when click download button ,set the iframe src to you download action
if everything is ok,return File(...)
if has some error ,return View(...)
and the view show in iframe,you can use some js to control the main page in iframe page to popup

main page

function pop(){....}  

iframe page

$(function(){ window.parent.pop(); });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.