Esempio n. 1
0
        public ActionResult Put(int id, [FromForm] ArticleImageUpload articleImageUpload)
        {
            if (articleImageUpload.ImageFile != null)
            {
                var ext = Path.GetExtension(articleImageUpload.ImageFile.FileName);

                if (!FileUpload.AllowedExtensions.Contains(ext))
                {
                    return(UnprocessableEntity("Image extension is not allowed."));
                }
            }

            try
            {
                var articleDto = new ArticleDto
                {
                    Title    = articleImageUpload.Title,
                    Content  = articleImageUpload.Content,
                    AuthorId = articleImageUpload.AuthorId
                };

                if (articleImageUpload.ImageFile != null)
                {
                    var newFileName = Guid.NewGuid().ToString() + "_" + articleImageUpload.ImageFile.FileName;
                    var filePath    = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "img", "article", newFileName);
                    articleImageUpload.ImageFile.CopyTo(new FileStream(filePath, FileMode.Create));
                    articleDto.Image = "img/article/" + newFileName;
                }

                _editArticleCommand.Execute((id, articleDto));
                return(NoContent());
            }
            catch (EntityNotFoundException e)
            {
                return(NotFound(e.Message));
            }
            catch (EntityAlreadyExistsException e)
            {
                return(Conflict(e.Message));
            }
            catch (Exception e)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, e.Message));
            }
        }
Esempio n. 2
0
        public IHttpActionResult UploadImageContent()
        {
            ArticleImageUpload uploadResponse = new ArticleImageUpload();

            try
            {
                var    postedFile = HttpContext.Current.Request.Files[0];
                string dateString = DateTime.Now.ToString("yyyyMMddHmmss");
                string fileName   = dateString + "-" + postedFile.FileName;
                var    path       = Path.Combine(HttpContext.Current.Server.MapPath("~/Upload/Article/Content/"), fileName);
                postedFile.SaveAs(path);

                uploadResponse.uploaded = 1;
                uploadResponse.fileName = fileName;
                uploadResponse.url      = WebConfigure.GetDomain() + "/Upload/Article/Content/" + fileName;
                return(Ok(uploadResponse));
            }
            catch (Exception e)
            {
                return(Ok(uploadResponse.error = e.InnerException?.ToString()));
            }
        }