Lujain Mohammad

Lujain Mohammad

  • NA
  • 10
  • 382

Inserting Image Error!

Mar 12 2020 3:57 PM
hello all, 
i tried to upload image to database and then retrieve it from database, 
when i tried to upload it, the webapp is stop debugging and didn't upload the image
here's my model
  1. public class Drink  
  2.     {  
  3.         public int DrinkId { getset; }  
  4.   
  5.         [Display(Name = "Drink Name")]  
  6.         public string DrinkName { getset; }  
  7.   
  8.         [Display(Name = "Company Name")]  
  9.         public string DrinkCompany { getset; }  
  10.   
  11.         [Display(Name = "Certificate")]  
  12.         public string CertificateDrinkUrl { getset; }  
  13.   
  14.         [NotMapped]  
  15.         public HttpPostedFileBase FileBase { getset; }  
  16.     }  
and here's my controller
  1. public class DrinkController : Controller  
  2.    {  
  3.        IRepositoryBase drinks;  
  4.   
  5.        public DrinkController(IRepositoryBase drinks)  
  6.        {  
  7.            this.drinks = drinks;  
  8.        }  
  9.   
  10.        // GET: Drink  
  11.        public ActionResult Index()  
  12.        {  
  13.            return View();  
  14.        }  
  15.   
  16.   
  17.        [HttpGet]  
  18.        public ActionResult AddDrink()  
  19.        {  
  20.            var model = new Drink();  
  21.   
  22.            return View(model);  
  23.        }  
  24.   
  25.        [HttpPost]  
  26.        public ActionResult AddDrink(Drink drink)  
  27.        {  
  28.            string fileName = Path.GetFileNameWithoutExtension(drink.FileBase.FileName);  
  29.   
  30.            string extension = Path.GetExtension(drink.FileBase.FileName);  
  31.   
  32.            fileName = fileName + DateTime.Now.ToString("yymmssffff") + extension;  
  33.   
  34.            drink.CertificateDrinkUrl = "~/Images/" + fileName;  
  35.   
  36.            fileName = Path.Combine(Server.MapPath("~/Images/"), fileName);  
  37.   
  38.            drink.FileBase.SaveAs(fileName);  
  39.   
  40.            using (DataContext db = new DataContext())  
  41.            {  
  42.                db.Drinks.Add(drink);  
  43.                db.SaveChanges();  
  44.            }  
  45.   
  46.            ModelState.Clear();  
  47.   
  48.            return View();  
  49.        }  
and finally here's my view
  1. @model WorldOfHalal.Models.Drink    
  2.     
  3. @{    
  4.     ViewBag.Title = "AddDrink";    
  5. }    
  6.     
  7. <h2>Add Drink</h2>    
  8.     
  9. @using (Html.BeginForm("AddDrink", "Drink", FormMethod.Post, new {enctype="multipart/form-data"}))     
  10. {    
  11.     @Html.AntiForgeryToken()    
  12.         
  13.     <div class="form-horizontal">    
  14.         <hr />    
  15.         @Html.ValidationSummary(true, "", new { @class = "text-danger" })    
  16.         <div class="form-group">    
  17.             @Html.LabelFor(model => model.DrinkName, htmlAttributes: new { @class = "control-label col-md-2" })    
  18.             <div class="col-md-10">    
  19.                 @Html.EditorFor(model => model.DrinkName, new { htmlAttributes = new { @class = "form-control" } })    
  20.                 @Html.ValidationMessageFor(model => model.DrinkName, "", new { @class = "text-danger" })    
  21.             </div>    
  22.         </div>    
  23.     
  24.         <div class="form-group">    
  25.             @Html.LabelFor(model => model.DrinkCompany, htmlAttributes: new { @class = "control-label col-md-2" })    
  26.             <div class="col-md-10">    
  27.                 @Html.EditorFor(model => model.DrinkCompany, new { htmlAttributes = new { @class = "form-control" } })    
  28.                 @Html.ValidationMessageFor(model => model.DrinkCompany, "", new { @class = "text-danger" })    
  29.             </div>    
  30.         </div>    
  31.     
  32.         <div class="form-group">    
  33.             @Html.LabelFor(model => model.CertificateDrinkUrl, htmlAttributes: new { @class = "control-label col-md-2" })    
  34.             <div class="col-md-10">    
  35.                 <input type="file" name="FileBase" required=""/>    
  36.             </div>    
  37.         </div>    
  38.     
  39.         <div class="form-group">    
  40.             <div class="col-md-offset-2 col-md-10">    
  41.                 <input type="submit" value="Create" class="btn btn-default" />    
  42.             </div>    
  43.         </div>    
  44.     </div>    
  45. }    
  46.     
  47. <div>    
  48.     @Html.ActionLink("Back to List", "DrinkList", null, new {@class = "btn btn-primary"})    
  49. </div>   
could anyone help me please? 

Answers (3)