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.

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);
        }
    }
}
@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>

}