I have a Product table with columns P_id(pk), Name(nvarchar(100)), Quantity(int), Price(decimal(10,2)), ImagePath1(nvarchar(100)), ImagePath2(nvarchar(100)), ImagePath3(nvarchar(100)), ImagePath4(nvarchar(100))

I only want to upload 4 images. Right now I'm doing it like this and I know this isn't good practice. I'm a beginner. Please tell me the efficient way to do this ? also I don't want to upload Image size > 1mb.

View

@using (Html.BeginForm("AddProduct", "Store", FormMethod.Post, new { enctype = "multipart/form-data",   @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Create a new product.</h4>
<hr />
@Html.ValidationSummary(true)

<div class="form-group">
    @Html.LabelFor(m => m.Name, new { @class = "col-md-2 control-label")
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
        @Html.ValidationMessageFor(m=>m.Name)
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.Price, new { @class = "col-md-2 control-label")
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.Price, new { @class = "form-control" })
        @Html.ValidationMessageFor(m=>m.Price)
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.Name, new { @class = "col-md-2 control-label")
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
        @Html.ValidationMessageFor(m=>m.Name)
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.ImagePath1, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        <input type="file" name="file1" id="file" style="width: 100%;" />
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.ImagePath2, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        <input type="file" name="file2" id="file" style="width: 100%;" />
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.ImagePath3, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        <input type="file" name="file3" id="file" style="width: 100%;" />
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.ImagePath4, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        <input type="file" name="file4" id="file" style="width: 100%;" />
    </div>
</div>
<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" class="btn btn-default" value="Create Product" />
    </div>
</div>

Controller

[HttpPost]
public ActionResult AddProduct(HttpPostedFileBase file1, HttpPostedFileBase file2, HttpPostedFileBase file3, HttpPostedFileBase file4)
{
   var allowedExtensions = new[] { ".jpg", ".png", ".jpeg", ".gif", ".JPG", ".PNG", ".JPEG" };
   var extension1 = System.IO.Path.GetExtension(file1.FileName);
   var extension2 = System.IO.Path.GetExtension(file2.FileName);
   var extension3 = System.IO.Path.GetExtension(file3.FileName);
   var extension4 = System.IO.Path.GetExtension(file4.FileName);
   if (allowedExtensions.Contains(extension1) ||allowedExtensions.Contains(extension2)|| allowedExtensions.Contains(extension3) || allowedExtensions.Contains(extension4)
       {
           string Image1 = System.IO.Path.GetFileName(file1.FileName);
           string Image2 = System.IO.Path.GetFileName(file2.FileName);
           string Image3 = System.IO.Path.GetFileName(file3.FileName);
           string Image4 = System.IO.Path.GetFileName(file4.FileName);

           string physicalPath1 = Server.MapPath("~/ProductImages/" + Image1);
           string physicalPath2 = Server.MapPath("~/ProductImages/" + Image2);
           string physicalPath3 = Server.MapPath("~/ProductImages/" + Image3);
           string physicalPath4 = Server.MapPath("~/ProductImages/" + Image4);

           file1.SaveAs(physicalPath1);
           file2.SaveAs(physicalPath2);
           file3.SaveAs(physicalPath3);
           file4.SaveAs(physicalPath4);

           Product newRecord = new Product();
           newRecord.Name = Request.Form["Name"];
           newRecord.Price = Request.Form["Price"];
           newRecord.Quantity = Request.Form["Quantity"];
           newRecord.ImagePath1 = ImagePath1;
           newRecord.ImagePath2 = ImagePath2;
           newRecord.ImagePath3 = ImagePath3;
           newRecord.ImagePath4 = ImagePath4;
           db.Products.Add(newRecord);
           db.SaveChanges();
           return RedirectToAction("Index", "Home");
      }
}

Related posts

Recent Viewed