Beispiel #1
0
        public ActionResult UploadExcelForClient(string name, string type = "Xlsx")
        {
            var fileName = Guid.NewGuid().ToString("N");
            var filePath = Server.MapPath(Settings.Current["TemporaryFolderForImportExport"]);

            ExcelType excelType;

            ExcelType.TryParse(type, true, out excelType);

            try
            {
                if (Request.Files.Count != 1 || Request.Files[0] == null)
                {
                    return new ContentResult
                           {
                               ContentType = "text/html",
                               Content     = DynamicEntityJSONDataSource.GetNotSuccess("Неверное количество файлов в запросе.")
                           }
                }
                ;


                var ds = ExcelHelper.FromExcel(Request.Files[0].InputStream, fileName, filePath, excelType);

                List <dynamic> records;
                var            result = ImportExportHelper.ImportForClient(name, ds, out records);
                var            report = result.GetReport(new ImportResultTextFormatter());

                var res = new
                {
                    success = !(result.HaveMappingErrors || result.HaveParsingErrors),
                    message = report,
                    records = records.Select(c => (c as DynamicEntity).Dictionary).ToArray()
                };

                return(new ContentResult
                {
                    ContentType = "text/html",
                    Content = ServiceStack.Text.JsonSerializer.SerializeToString(res)
                });
            }
            catch (Exception ex)
            {
                Logger.Log.Error(string.Format("Ошибка загрузки шаблона {0}", name), ex);

                return(new ContentResult
                {
                    ContentType = "text/html",
                    Content = DynamicEntityJSONDataSource.GetNotSuccess(ex.Message)
                });
            }
            finally
            {
                try
                {
                    var fullName = ExcelHelper.GetFullFilePath(fileName, filePath, excelType);

                    if (System.IO.File.Exists(fullName))
                    {
                        System.IO.File.Delete(fullName);
                    }
                }
                catch (Exception ex)
                {
                    Logger.Log.Error("Ошибка удаления временного файла", ex);
                }
            }
        }