<#@ template language="C#" HostSpecific="True" #>
<#@ output extension="cs" #>
<#@ include file="..\Share\Head.include.t4" #>
<#@ parameter name="C1JsonContext" type="System.String" #>
<#@ parameter name="RemoteSaveFileActionName" type="System.String" #>
<#@ parameter name="SavePath" type="System.String" #>
<#@ parameter name="FilePath" type="System.String" #>
<#@ parameter name="WebRootPathName" type="System.String" #>

<#
        string savePath = !String.IsNullOrEmpty(SavePath) ? SavePath : FilePath;
        if(savePath.StartsWith("~/"))
        {
            savePath = savePath.Substring(2);
        }
#>
        public JsonResult <#= RemoteSaveFileActionName #>([FlexSheetRequest]FlexSheetSaveRequest request)
        {
            var success = true;
            var error = "";
            var savePath = Path.Combine(<#= WebRootPathName #>, @"<#= savePath #>");
            try
            {
                Stream st = request.GetFileStream();
                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);
                    }
                }
            }
            catch (Exception e)
            {
                success = false;
                error = e.ToString();
            }

            return <#= C1JsonContext #>.C1Json(FlexSheetHelper.Save(success, error));
        }