Hi,
i want to uplaod image and save it into database according to their id.
i tried my code but unfortunately, it didn't work. When I click Upload, nothing happen.
Model class
public class ItemModel
{
public int item_id { get; set; }
public string Form_Id { get; set; }
public Nullable<int> id { get; set; }
public string auditeeAttach { get; set; }
public HttpPostedFileBase file1 { get; set; }
}
Controller
[HttpGet]
public ActionResult ImageProven(ItemModel imageIn)
{
string id = imageIn.Form_Id;
ViewBag.formId = id;
return View();
}
public ActionResult ImageProven(HttpPostedFileBase file1, ItemModel imageIn)
{
string id = imageIn.Form_Id.ToString();
if (imageIn != null)
{
var addImage = db.tbl_detailItem.Where(z => z.item_id == imageIn.item_id).FirstOrDefault();
if (file1 != null)
{
var allowedExtensions = new[]
{
".Jpg", ".png", ".jpg", "jpeg",".pptx"
};
var fileName = Path.GetFileName(imageIn.file1.FileName); //getting only file name
var ext = Path.GetExtension(imageIn.file1.FileName); //getting the extension(ex-.jpg)
if (allowedExtensions.Contains(ext)) //check what type of extension
{
string name = Path.GetFileNameWithoutExtension(fileName); //getting file name without extension
string myfile = name +/* "Inn_" + count.ToString().PadLeft(3, '0') +*/ ext;
// store the file inside ~/project folder(Img)
var path = Path.Combine(Server.MapPath("~/ImageStore/form/"), myfile);
addImage.auditeeAttach = "~/ImageStore/form/" + myfile;
file1.SaveAs(path);
if (String.IsNullOrEmpty(addImage.auditeeAttach))
{
addImage.auditeeAttach = "~/ImageStore/form/";
}
}
else
{
ViewBag.message = "Please choose only Image file or Email File";
return View();
}
}
db.SaveChanges();
ViewBag.formId = id;
}
return RedirectToAction("AuditeeListItemDetail", "Auditee", new { id });
}
View
@using (Html.BeginForm("ImageProven", "Auditee", FormMethod.Post, new { @class = "form-horizontal", role = "form", enctype = "multipart/form-data", id = "file1", name = "file1" }))
{
<td data-name="attachment" id="auditeeAttach">
<!-- file upload start-->
<div>
<label class="custom-file-upload form-control">
<i class="fa fa-cloud-upload"></i>
<input name="file1" type="file" id="file1" required>
</label>
</div>
<!-- file upload ends-->
<input class="form-control " id="item_id" name="item_id" type="hidden" value="@item.item_id">
<input class="form-control " id="Form_Id" name="Form_Id" type="hidden" value="@item.Form_Id">
<button type="submit" style="float:right" class="btn btn-success" >Upload</button>
</td>
}