c# - Prompt the user to download an excel file built server side -


I have a way of calling my CreateAnnuityExcel sheet when pressed on the "Create button".

Public ActionResult ShowAnnuityPMT (FormCollection is the form, string SubmitValue, name of the string file) {// to finalList, the code (SubmitValue = = "Save") {CreateAnnuityExcelSheet (finalList: Method To CreateAnnuityExcelSheet Call, Form, DTCIer); // When I call this method, I want to download the excel file created in the user / browser CreateAnnuityExcelSheet} Return the partial view ("ShowDetail", the last list); }

The method looks like this:

  Public Zero CreateAnnuityExcelSheet (list of calculation> cList, formCollection as intDTCyear) {list From & lt; Count & gt; NewList = New list & lt; Count & gt; (); NewList.Add (cList.First ()); // Get value for the first line StartValue = newList [0] .StartValue; Var Radio = Form ["Advance Orories"]; String filename = newest list [0] .CalculationName; String path = @ "c: \ excel file \" + filename + ".xlsx"; // file file information information for file = new file info (path); Info.directory.Create (); // So C: (! Info.Exists) \ ExcelFiles does not exist if it's created by using {ExcelPackage package = new ExcelPackage (info)} {ExcelWorksheet ws = package.Workbook.Worksheets.Add (file name) ; // Styles for Sheet Stream = new memorystream (); package.SaveAs (stream); section. Composition = 0; Byte [] bytes = stream Getbuffer (); reaction. Buffer = true; Response.Clear (); Response.ContentType = "app / xlsx"; Response.AddHeader ("content-type", "attachment; file name = report.xlsx"); Response.BinaryWrite (byte); Response.Flush (); }   

The main problem is that I am creating an Excelsheet on the server side and then want to send it back in the browser to download it. How can I send my package back to the browser to download?

The method is returning zero now, should I return the file? But if I do, then how do I send the file with a view, so that the user can begin downloading the Excel file?

This shows my view: This displays my view < / Div>

Your action should return an ActionResult and if you have a stream, then an easy way

There is no asset to set the name of the notice file to use.

  New filestream result (stream, "app / vnd.ms-excel") {FileDownloadName = "myfile.xslx"};   

Here's a sample to post data to your form and download the generated file.

Create a model to keep form values:

  public class DownloadModel {public int InputA {get; Set; } Public String InputB {get; Set; }}   

Create a controller with two actions: one to display a form (index) and another to submit the data and to get the file (downloadfile)

  public class DownloadController: controller {public performance index () {return view (); } Public Performance DownloadFile (DownloadModel Models) {// Read Form Price Through Module System. Diagnostics Debug.WriteLine (model.InputA); System.Diagnostics.Debug.WriteLine (model.InputB); // Assume that we create an Excel stream ... MemoryStream excelStream = new MemoryStream (); New FileStream Receipt Return (excelsstream, "app / vnd.ms-excel") {FileDownloadName = "myfile.xslx"}; }}   

Razor Scene Download Action Form / Index.cshtml for DownloadFile Action in Set

  @model WebApp.Models.DownloadModel @ {ViewBag Titles = "Download file"; } & Lt; H2 & gt; Download file & lt; / H2 & gt; @Usage (Html.BeginForm ("DownloadFile", "Downloads")) {@ Html.TextBoxFor (model = & gt; model.InputA); @ Html.TextBoxFor (model = & gt; model.InputB); & Lt; Input type = "submit" value = "save" name = "submit button" /> }    

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -