Ejemplo n.º 1
0
        public ActionResult Upload(FileUploadViewModel model, string command)
        {
            Server.ScriptTimeout = 600;     // Allow 10 minutes for an upload/import

            if (command.ToLower() == "upload")
            {
                if (model.Files != null && model.Files.Count() > 0)
                {
                    var targetFile = Path.GetTempPath();

                    var    attachment = model.Files.FirstOrDefault();
                    string fileName   = attachment.FileName;

                    if (!MediaServices.IsValidOrderImportType(fileName))
                    {
                        model.SetError(ErrorIcon.Error, EvolutionResources.errInvalidImportFile.Replace("%1", MediaServices.GetValidOrderImportTypes()));
                    }
                    else if (attachment != null && attachment.ContentLength > 0)
                    {
                        try {
                            targetFile += fileName.FileName();
                            attachment.SaveAs(targetFile);

                            var error = FileImportService.UploadFile(CurrentCompany, CurrentUser, targetFile,
                                                                     model.FirstLineContainsHeader);
                            if (error.IsError)
                            {
                                model.SetError(ErrorIcon.Error, error.Message);
                            }
                            else
                            {
                                // Display the table
                                var mappingsModel = new EditSaleImportDataMappingsViewModel();
                                prepareViewModel(mappingsModel);

                                mappingsModel.Data = FileImportService.GetData(CurrentCompany, CurrentUser);

                                SalesService.ValidateOrders(CurrentCompany, CurrentUser, mappingsModel.Data.Headings, mappingsModel.DisplayDateFormat);

                                mappingsModel.Data = FileImportService.GetData(CurrentCompany, CurrentUser);

                                return(View("ImportMappings", mappingsModel));
                            }
                        } catch (Exception e1) {
                            model.SetError(ErrorIcon.Error, EvolutionResources.errFailedToUploadFile.Replace("%1", fileName).Replace("%2", e1.Message));
                            LogService.WriteLog(e1, Request.RawUrl);
                        }
                    }

                    prepareViewModel(model);
                    return(View("SaleImport", model));
                }
            }

            return(RedirectToAction("Index"));
        }
        public ActionResult Upload(FileUploadViewModel model, string command)
        {
            Server.ScriptTimeout = 600;     // Allow 10 minutes for an upload/import

            if (command.ToLower() == "upload")
            {
                if (model.Files != null || model.Files.Count() == 1 &&
                    model.Images != null || model.Images.Count() == 1)
                {
                    var    targetFolder = Path.GetTempPath();
                    string prodFile     = "",
                           zipFile      = "";

                    try {
                        var attachment = model.Files.FirstOrDefault();
                        if (attachment != null)
                        {
                            string fileName = attachment.FileName;

                            if (!MediaServices.IsValidOrderImportType(fileName))
                            {
                                model.SetError(ErrorIcon.Error, EvolutionResources.errInvalidImportFile.Replace("%1", MediaServices.GetValidOrderImportTypes()));
                            }
                            else if (attachment.ContentLength > 0)
                            {
                                prodFile = targetFolder + fileName.FileName();
                                attachment.SaveAs(prodFile);
                            }

                            if (!model.Error.IsError)
                            {
                                attachment = model.Images.FirstOrDefault();
                                if (attachment != null)
                                {
                                    fileName = attachment.FileName;

                                    if (fileName.FileExtension().ToLower() != "zip")
                                    {
                                        model.SetError(ErrorIcon.Error, EvolutionResources.errInvalidImportFile.Replace("%1", "zip"));
                                    }
                                    else if (attachment.ContentLength > 0)
                                    {
                                        zipFile = targetFolder + fileName.FileName();
                                        attachment.SaveAs(zipFile);

                                        MembershipManagementService.SaveProperty("ProdImportZip", zipFile);
                                    }
                                }
                            }
                        }
                    } catch (Exception e1) {
                        model.Error.SetError(e1);
                    }

                    if (!model.Error.IsError)
                    {
                        try {
                            model.Error = FileImportService.UploadFile(CurrentCompany, CurrentUser, prodFile,
                                                                       model.FirstLineContainsHeader);
                            if (!model.Error.IsError)
                            {
                                // Display the table
                                var mappingsModel = new EditDataMappingViewModel();
                                prepareViewModel(mappingsModel);

                                mappingsModel.Data = FileImportService.GetData(CurrentCompany, CurrentUser);

                                ProductService.ValidateProducts(CurrentCompany, CurrentUser, mappingsModel.Data.Headings);
                                mappingsModel.Data = FileImportService.GetData(CurrentCompany, CurrentUser);

                                return(View("ImportMappings", mappingsModel));
                            }
                        } catch (Exception e1) {
                            model.SetError(ErrorIcon.Error, EvolutionResources.errFailedToUploadFile.Replace("%1", prodFile).Replace("%2", e1.Message));
                            LogService.WriteLog(e1, Request.RawUrl);
                        }
                    }

                    MediaService.MediaService.DeleteFile(prodFile);
                    MediaService.MediaService.DeleteFile(zipFile);

                    PrepareViewModel(model, EvolutionResources.bnrProductImport, 0, 0);
                    return(View("ProductImport", model));
                }
            }

            return(RedirectToAction("Index"));
        }