예제 #1
0
        public async Task <ActionResult> Upload(HttpPostedFileBase file)
        {
            //check the user has entered a file
            if (file != null)
            {
                //check if the file is valid
                if (ValidateFile(file))
                {
                    try
                    {
                        SaveFileToDisk(file);
                    }
                    catch (Exception)
                    {
                        ModelState.AddModelError("FileName", "Sorry an error occurred saving the file to disk, please try again");
                    }
                }
                else
                {
                    ModelState.AddModelError("FileName", "The file must be gif, png, jpeg or jpg and less than 2MB in size");
                }
            }
            else
            {
                //if the user has not entered a file return an error message
                ModelState.AddModelError("FileName", "Please choose a file");
            }

            if (ModelState.IsValid)
            {
                var model = new ItemImageCreate
                {
                    FileName = file.FileName
                };

                var itemImageService = CreateItemImageService();

                string responce = await itemImageService.CreateItemImage(model);

                if (responce == "success")
                {
                    TempData["SaveResult"] = "Your file was uploaded.";
                    return(RedirectToAction("Index"));
                }

                ModelState.AddModelError("FileName", responce);
            }

            return(View());
        }
예제 #2
0
        public async Task <string> CreateItemImage(ItemImageCreate model)
        {
            var entity =
                new ItemImage()
            {
                OwnerId  = _userId,
                FileName = model.FileName
            };

            using (var ctx = new ApplicationDbContext())
            {
                string message = "";
                ctx.ItemImages.Add(entity);
                try
                {
                    if (await ctx.SaveChangesAsync() == 1)
                    {
                        message = "success";
                    }
                }
                catch (DbUpdateException ex)
                {
                    SqlException innerException = ex.InnerException.InnerException as SqlException;
                    if (innerException != null && innerException.Number == 2601)
                    {
                        message = "The file " + model.FileName +
                                  " already exists in the system. Please delete it and try again if you wish to re-add it";
                    }
                    else
                    {
                        message = "Sorry an error has occurred saving to the database, please try again";
                    }
                }
                return(message);
            }
        }