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".
The method looks like this: 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: 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. Here's a sample to post data to your form and download the generated file. Create a model to keep form values: Create a controller with two actions: one to display a form (index) and another to submit the data and to get the file (downloadfile) Razor Scene Download Action Form / Index.cshtml for DownloadFile Action in Set
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 (); }
< / Div>
New filestream result (stream, "app / vnd.ms-excel") {FileDownloadName = "myfile.xslx"};
public class DownloadModel {public int InputA {get; Set; } Public String InputB {get; Set; }}
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"}; }}
@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
Post a Comment