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.C1Excel;
using C1.Web.Mvc.Serialization;
using C1.Web.Mvc.Sheet;
using FlexSheetExplorer.Models;
using System;
using System.IO;
using System.Web.Mvc;

namespace FlexSheetExplorer.Controllers
{
    public partial class FlexSheetController : Controller
    {
        private const string _FILE_PATH = "~/Content/uploadFile/save.xlsx";

        public ActionResult RemoteLoadSave(string loadType)
        {
            ViewBag.LoadSaveTypes = new string[] { "Xlsx", "Workbook" };

            if (loadType == "Workbook")
            {
                ViewBag.LoadAction = "RemoteLoadWorkbook";
            }
            else
            {
                loadType = "Xlsx";
                ViewBag.LoadAction = "RemoteLoadXlsx";
            }
            ViewBag.LoadType = loadType;

            return View();
        }

        public ActionResult RemoteLoadWorkbook() {
            return this.C1Json(FlexSheetHelper.Load( WorkbookOM.GetWorkbook()),
                   null, null, JsonRequestBehavior.AllowGet);
        }

        public ActionResult RemoteLoadXlsx() {
            return this.C1Json(FlexSheetHelper.Load("~/Content/xlsxFile/example1.xlsx"),
                   null, null, JsonRequestBehavior.AllowGet);
        }

        public JsonResult RemoteSaveFile([FlexSheetRequest]FlexSheetSaveRequest request)
        {
            var success = true;
            var error = "";
            var savePath = Server.MapPath(_FILE_PATH);
            try
            {
                Stream st = request.GetFileStream();
                Workbook wb = request.Workbook;
                using (FileStream fs = new FileStream(savePath, FileMode.Create))
                {
                    if (st != null)
                    {
                        byte[] byteArray = new Byte[st.Length];
                        st.Read(byteArray, 0, (int)st.Length);
                        fs.Write(byteArray, 0, byteArray.Length);
                    }
                    else if (wb != null)
                    {
                        wb.ToC1XLBook().Save(fs, FileFormat.OpenXml);
                    }
                }
            }
            catch (Exception e)
            {
                success = false;
                error = e.ToString();
            }

            return this.C1Json(FlexSheetHelper.Save(success, error));
        }

        public FileResult DownloadFile()
        {
            var name = Path.GetFileName(_FILE_PATH);
            return File(Server.MapPath(_FILE_PATH), "application/msexcel", Url.Encode(name));
        }
    }
}
@section Scripts{
    <script>
        function remoteSave() {
            var combo = wijmo.Control.getControl('#saveType');
            var flexSheet = wijmo.Control.getControl('#flexSheet');
            flexSheet.remoteSave(c1.mvc.grid.sheet.ContentType[combo.selectedValue]);
        }

        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(Resources.FlexSheet.RemoteLoadSave_Text4)</h3>

        <p>@Html.Raw(Resources.FlexSheet.RemoteLoadSave_Text0)</p>

        <p>
            @using (Html.BeginForm())
            {
                @(Html.C1().ComboBox().Name("loadType").Bind((string[])ViewBag.LoadSaveTypes)
                            .SelectedValue((string)ViewBag.LoadType))
                <button type="submit" class="btn btn-default">@Html.Raw(Resources.FlexSheet.RemoteLoadSave_Text6)</button>
            }
        </p>

        <p>
            @(Html.C1().ComboBox().Id("saveType").Bind((string[])ViewBag.LoadSaveTypes))
            <button type="button" class="btn btn-default" onclick="remoteSave()">@Html.Raw(Resources.FlexSheet.RemoteLoadSave_Text5)</button>
        </p>

    </div>
    <div>
        @(Html.C1().FlexSheet().CssClass("flexSheet").Id("flexSheet")
        .RemoteLoad(Url.Action((string)ViewBag.LoadAction))
        .RemoteSave(Url.Action("RemoteSaveFile"))
        .OnClientRemoteSaved("onFileSaved"))
    </div>
</div>
@section Summary{
<p>@Html.Raw(Resources.FlexSheet.RemoteLoadSave_Text3)</p>

}