Ejemplo n.º 1
0
        public ActionResult EnviarImagemProduto(Int32 idProduto, String tempFile)
        {
            var jsonResult = new JsonRequestResult();
            try
            {
                // Verifica os Diretorios
                VerifyDirectories(TipoArquivo.Imagem);

                // Verifica se foi postado alguma coisa
                if (Request.Files.Count > 0)
                {
                    foreach (String file in Request.Files)
                    {
                        var postedFile = Request.Files[file];
                        if (postedFile != null && postedFile.ContentLength > 0)
                        {
                            // Verifica a extensão
                            if (!HasExtension(TipoArquivo.Imagem, postedFile.FileName))
                            {
                                jsonResult.Message = String.Format(Constants._msgUnauthorizedExtension, Path.GetExtension(postedFile.FileName), Settings.ImgAllowedExtensions);
                                jsonResult.ResultType = JsonRequestResultType.Alert;
                            }

                            // Verifica o Tamanho do Arquivo
                            if (!VerifyFileSize(postedFile.ContentLength))
                            {
                                jsonResult.Message = String.Format(Constants._msgFileSizeExceeded, Settings.MaxFileSize);
                                jsonResult.ResultType = JsonRequestResultType.Alert;
                            }

                            // Arquivo temporário
                            tempFile = Path.GetFileName(String.Format("imgProd{0}{1}", Path.GetFileNameWithoutExtension(postedFile.FileName), Path.GetExtension(postedFile.FileName)));
                            var pathProductFromServer = Path.Combine(Server.MapPath(Settings.UrlGalleryProducts), tempFile);

                            // Verificar se existe arquivo com mesmo Nome e o elimina
                            if (System.IO.File.Exists(pathProductFromServer))
                            {
                                System.IO.File.Delete(pathProductFromServer);
                            }

                            // Salva o arquivo no Disco do Servidor
                            postedFile.SaveAs(pathProductFromServer);

                            var pathDetailFromServer = Path.Combine(Server.MapPath(Settings.UrlGalleryProductsDetails), tempFile);
                            var pathThumbFromServer = Path.Combine(Server.MapPath(Settings.UrlGalleryProductsThumbs), tempFile);

                            // Resize da Imagem Details
                            GeneralHelpers.ResizeImage(pathProductFromServer,
                                                       pathDetailFromServer,
                                                       Settings.ImgWidthProductDetail,
                                                       Settings.ImgHeightProductDetail, true);

                            // Resize da Imagem Thumbs
                            GeneralHelpers.ResizeImage(pathProductFromServer,
                                                       pathThumbFromServer,
                                                       Settings.ImgWidthProductThumb,
                                                       Settings.ImgHeightProductThumb, true);

                            // Atualiza Objeto
                            if (idProduto != 0)
                            {
                                var tempObj = new ProdutoAnexoService().GetImage(idProduto);
                                tempObj.CaminhoAnexo = tempFile;
                                new ProdutoAnexoService().UpdateObject(tempObj);
                            }
                        }
                        else
                        {
                            jsonResult.Message = Constants._msgFileNotFound;
                            jsonResult.ResultType = JsonRequestResultType.Alert;
                        }
                    }
                }
                else
                {
                    jsonResult.Message = Constants._msgFileNotFound;
                    jsonResult.ResultType = JsonRequestResultType.Alert;
                }
            }
            catch (Exception ex)
            {
                LogService.Log("UploadController.EnviarImagemProduto()", ex);

                jsonResult.Message = Constants._msgError;
                jsonResult.Description = CustomException.GetInnerException(ex).Message;
                jsonResult.ResultType = JsonRequestResultType.Error;
            }
            // Serializará o JsonResult para ser exibido em um Alert do sistema
            var dict = new JavaScriptSerializer().Serialize(jsonResult);
            return RedirectToAction("EnviarImagemProduto",
                                    new RouteValueDictionary(
                                        new
                                        {
                                            controller = "Upload",
                                            action = "EnviarImagemProduto",
                                            id = idProduto,
                                            errorMessage = dict,
                                            fileName = tempFile
                                        }));
        }
Ejemplo n.º 2
0
        public ActionResult EnviarImagemProduto(Int32? id, String errorMessage, String fileName)
        {
            // Primeiro deve ser verificado se houve erros ou alertas
            if (!String.IsNullOrWhiteSpace(errorMessage))
            {
                var jsonResult = ((JsonRequestResult)new JavaScriptSerializer().Deserialize(errorMessage, typeof(JsonRequestResult)));
                if (jsonResult.Message != null)
                {
                    ViewBag.ErrorMessage = errorMessage;
                }
            }
            // Pegar o Nome da Imagem enviada e Grava no ViewBag
            if (!String.IsNullOrWhiteSpace(fileName))
            {
                ViewBag.TempFile = fileName;
                ViewBag.Nome = fileName;
            }

            if (id != null && id != 0) // Update
            {
                var tempObj = new ProdutoAnexoService().GetImage(id.Value);
                if (tempObj != null)
                {
                    // Busca somente a Primeira imagem ativa
                    ViewBag.TempFile = tempObj.CaminhoAnexo;
                    //ViewBag.Nome = tempObj.Ecommerce_Produto.Nome;
                    return View(tempObj);
                }
            }

            return View();
        }