Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I return a FileStreamResult from a View in MVC
    primarykey
    data
    text
    <p>In my Controller I have a GenerateDocument method that returns a FileStreamResult.</p> <p>In my view I have javascript that simply opens a new like so; window.open('/controller/GenerateDocument/?id=blah');</p> <p>I need my GenerateDocument method to: 1. return a pdf when the document can be created/fetched without errors, 2. return a new page/view when there are errors.</p> <p>Here's the Controller code:</p> <pre><code> public FileStreamResult GenerateDocument(int id) { string errorResult = string.Empty; try { DocumentDetail documentDetail = DocumentGenerator.GenerateAgreements(id, DcmDocumentEntityType.Sponsor, GetAuthUser()); if (documentDetail == null) return null; Stream stream = DocumentGenerator.RetrieveFile(documentDetail); return new FileStreamResult(stream, "application/pdf"); } catch (Exception ex) { errorResult = FormatException(ex); Trace.TraceError(errorResult); } //ViewBag.Error = HttpUtility.HtmlEncode(errorResult).Replace("\n", "&lt;br /&gt;"); //return View("DocGenerationError"); return null; } </code></pre> <p>I tried using using ActionResult and returning the view in case of an error, but as I noted in a comment on this thread, in the page javascript, window.open(url) would not work right.</p> <p>Here's the view code:</p> <pre><code> $('#ActionMenu').change(function() { var action = $(this).val(); switch (action) { case 'Generate Sponsor Agreement': window.open('/Sponsor/GenerateDocument/?id=@(Model.SponsorID)'); break; other cases default: } }); </code></pre> <p>Note: the Controller code above WORKS. It's when I switch to returning an ActionResult and uncomment out the exception handling stuff that it stops working.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload