Example #1
0
        public ActionResult Add(PictureInputModel model)
        {
            if (ModelState.IsValid)
            {
                string urlToSaveInDb = this.SavePic(model.Picture);
                if (urlToSaveInDb != null)
                {
                    var picture = new Picture()
                    {
                        Title = model.Title,
                        Url = urlToSaveInDb,
                    };
                    this.Data.Pictures.Add(picture);
                    this.Data.SaveChanges();

                    return this.RedirectToAction("Add", "Home");
                }
                else
                {
                    this.ModelState.AddModelError(string.Empty, "Error while saving picture!");
                }
            }

            return this.View(model);
        }
Example #2
0
        public async Task<HttpResponseMessage> Add()
        {
            // Check if the request contains multipart/form-data.
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            string root = HttpContext.Current.Server.MapPath("~/Files/Images/");
            var provider = new MultipartFormDataStreamProvider(root);

            try
            {
                // Read the form data.
                await Request.Content.ReadAsMultipartAsync(provider);

                bool isSaved = false;
                var newName = string.Empty;
                Picture picture = new Picture();

                foreach (MultipartFileData file in provider.FileData)
                {
                    var fileName = file.Headers.ContentDisposition.FileName;
                    var extensionStartIndex = fileName.LastIndexOf('.') + 1;
                    var extensionEndIndex = fileName.LastIndexOf('"');
                    var extension = fileName.Substring(extensionStartIndex, extensionEndIndex - extensionStartIndex);
                    if (this.IsValidExtension(extension))
                    {
                        newName = file.LocalFileName + '.' + extension;
                        File.Move(file.LocalFileName, newName);
                        string url = newName.Substring(newName.LastIndexOf("\\") + 1);
                        picture.Url = url;
                        isSaved = true;
                    }
                    else
                    {
                        return Request.CreateResponse(HttpStatusCode.BadRequest);
                    }
                }

                bool isTitleCorrect = false;
                foreach (var key in provider.FormData.AllKeys)
                {
                    foreach (var val in provider.FormData.GetValues(key))
                    {
                        if (string.IsNullOrEmpty(val) == false) 
                        {
                            picture.Title = val;
                            isTitleCorrect = true;
                        }
                        else if (isSaved)
                        {
                            File.Delete(newName);
                            return Request.CreateResponse(HttpStatusCode.BadRequest);
                        }
                    }
                }

                if (isSaved && isTitleCorrect)
                {
                    this.Data.Pictures.Add(picture);
                    this.Data.SaveChanges();

                    return Request.CreateResponse(HttpStatusCode.OK);
                }

                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
            catch (System.Exception e)
            {
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
            }
        }