Features
Remote Load/Save
Remote Load/Save
Load xlsx file or Workbook from some action and save the file to the server.
Features
Remote Load/Save
This example demonstrates how to load xlsx file or Workbook from some action and save the file to the server.
In this example, if the "Save" button is clicked,
FlexSheet will send the file to server and the browser will download it from server automatically.
1
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
0
- Sheet1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | using C1.Web.Mvc.Serialization; using C1.Web.Mvc.Sheet; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using System; using System.IO; namespace FlexSheetExplorer.Controllers { public partial class FlexSheetController : Controller { private const string FILE_PATH = "Content\\xlsxFile\\RemoteSave.xlsx" ; private readonly string _webRootPath; public FlexSheetController(IHostingEnvironment hostingEnvironment) { _webRootPath = hostingEnvironment.WebRootPath; } public ActionResult RemoteLoadSave() { return View(); } public ActionResult RemoteLoadXlsx() { return this .C1Json(FlexSheetHelper.Load( "~/Content/xlsxFile/example1.xlsx" )); } public JsonResult RemoteSaveFile([FlexSheetRequest]FlexSheetSaveRequest request) { var success = true ; var error = "" ; var savePath = Path.Combine(_webRootPath, FILE_PATH); try { Stream st = request.GetFileStream(); using (FileStream fs = new FileStream(savePath, FileMode.Create)) { if (st != null ) { st.CopyTo(fs); } } } catch (Exception e) { success = false ; error = e.ToString(); } return this .C1Json(FlexSheetHelper.Save(success, error)); } public FileResult DownloadFile() { var savePath = Path.Combine(_webRootPath, FILE_PATH); var name = Path.GetFileName(FILE_PATH); return File( new FileStream(savePath, FileMode.Open, FileAccess.Read), "application/msexcel" , name); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | @section Scripts{ <script> function remoteSave() { var flexSheet = wijmo.Control.getControl( '#flexSheet' ); flexSheet.remoteSave(c1.mvc.grid.sheet.ContentType.Xlsx); } function onFileSaved(sender, args) { if (args.success) { window.location.href = '@Url.Action("DownloadFile")' ; } else { alert(args.error); } } </script> } < div > < div class = "copy" > < h3 > @Html .Raw(FlexSheetRes.RemoteLoadSave_Text2) </ h3 > < p > @Html .Raw(FlexSheetRes.RemoteLoadSave_Text0)</ p > </ div > < div > < c1-flex-sheet class = "flexSheet" id = "flexSheet" load-action-url = "@(Url.Action(" RemoteLoadXlsx "))" save-action-url = "@(Url.Action(" RemoteSaveFile "))" remote-saved = "onFileSaved" > </ c1-flex-sheet > </ div > </ div > @section Summary{ < p > @Html .Raw(FlexSheetRes.RemoteLoadSave_Text1)</ p > } |