Esempio n. 1
0
        public ActionResult Import(SandboxImportModel model)
        {
            try
            {
                if (model.ListStores == null)
                {
                    ModelState.AddModelError("ListStores", CurrentUser.GetLanguageTextFromKey("Please choose store."));
                    //return View(model);
                }
                //if (model.ImageZipUpload == null || model.ImageZipUpload.ContentLength <= 0)
                //{
                //    ModelState.AddModelError("ImageZipUpload", "Image Folder (.zip) cannot be null");
                //    return View(model);
                //}

                if (model.ImageZipUpload != null)
                {
                    if (!Path.GetExtension(model.ImageZipUpload.FileName).ToLower().Equals(".zip"))
                    {
                        ModelState.AddModelError("ImageZipUpload", "");
                        //return View(model);
                    }
                }
                if (model.ExcelUpload == null || model.ExcelUpload.ContentLength <= 0)
                {
                    ModelState.AddModelError("ImageZipUpload", CurrentUser.GetLanguageTextFromKey("Excel filename cannot be null"));
                    //return View(model);
                }

                if (!ModelState.IsValid)
                {
                    return(View(model));
                }

                FileInfo[] listFiles            = new FileInfo[] { };
                string     serverZipExtractPath = System.Web.HttpContext.Current.Server.MapPath("~/Uploads") + "/ExtractFolder";
                if (model.ImageZipUpload != null && model.ImageZipUpload.ContentLength > 0)
                {
                    bool   isFolderEmpty;
                    string fileName = Path.GetFileName(model.ImageZipUpload.FileName);
                    string filePath = string.Format("{0}/{1}", System.Web.HttpContext.Current.Server.MapPath("~/Uploads"), fileName);

                    //upload file to server
                    if (System.IO.File.Exists(filePath))
                    {
                        System.IO.File.Delete(filePath);
                    }
                    model.ImageZipUpload.SaveAs(filePath);

                    //extract file
                    CommonHelper.ExtractZipFile(filePath, serverZipExtractPath);

                    //delete zip file after extract
                    if (System.IO.File.Exists(filePath))
                    {
                        System.IO.File.Delete(filePath);
                    }
                    isFolderEmpty = CommonHelper.IsDirectoryEmpty(serverZipExtractPath);

                    if (!isFolderEmpty)
                    {
                        string[]      extensions = new[] { ".jpg", ".png", ".jpeg" };
                        DirectoryInfo dInfo      = new DirectoryInfo(serverZipExtractPath);
                        //Getting Text files
                        listFiles = dInfo.EnumerateFiles()
                                    .Where(f => extensions.Contains(f.Extension.ToLower()))
                                    .ToArray();
                    }
                }

                ImportModel importModel = new ImportModel();
                string      msg         = "";
                // read excel file
                if (model.ExcelUpload != null && model.ExcelUpload.ContentLength > 0)
                {
                    string fileName = Path.GetFileName(model.ExcelUpload.FileName);
                    string filePath = string.Format("{0}/{1}", System.Web.HttpContext.Current.Server.MapPath("~/Uploads"), fileName);

                    //upload file to server
                    if (System.IO.File.Exists(filePath))
                    {
                        System.IO.File.Delete(filePath);
                    }
                    model.ExcelUpload.SaveAs(filePath);

                    importModel = _factory.Import(filePath, listFiles, model.ListStores, ref msg);
                    //delete folder extract after get file.
                    if (System.IO.Directory.Exists(serverZipExtractPath))
                    {
                        System.IO.Directory.Delete(serverZipExtractPath, true);
                    }
                    //delete file excel after insert to database
                    if (System.IO.File.Exists(filePath))
                    {
                        System.IO.File.Delete(filePath);
                    }
                }
                if (msg.Equals(""))
                {
                    return(View("ImportDetail", importModel));
                }
                else
                {
                    _logger.Error("Customer_Import: " + msg);
                    ModelState.AddModelError("ImageZipUpload", msg);
                    return(View(model));
                }
            }
            catch (Exception e)
            {
                _logger.Error("Customer_Import: " + e);
                ModelState.AddModelError("ImageZipUpload", CurrentUser.GetLanguageTextFromKey("Import file have error."));
                return(View(model));
            }
        }