Hi,
based on my previous question in Upload image based on their id
I cannot save the image into database. After i clicked submit the image is not save into database.
Btw, im using the exact code from the previous question in the link above.
public ActionResult ImageProven(HttpPostedFileBase file1, ItemModel imageIn)
{
string id = imageIn.Form_Id.ToString();
try
{
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 RedirectToAction("AuditeeListItemDetail", "Auditee", new { id });
}
}
db.SaveChanges();
ViewBag.formId = id;
}
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
System.Diagnostics.Debug.WriteLine("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
}
}
}
return RedirectToAction("AuditeeListItemDetail", "Auditee", new { id });
}
<table id="datatable" class="table table-striped table-bordered dataTable" cellspacing="0" role="grid" aria-describedby="datatable_info" style="width: 100%; float:left">
<thead>
<tr>
<th>#ID</th>
<th>Item Name</th>
<th>Item Description</th>
<th>Attachment</th>
<th>Action Taken</th>
</tr>
</thead>
<tbody>
@if (ViewBag.listInfo != null)
{
foreach (var item in ViewBag.listInfo)
{
<tr>
<td>@item.Form_Id</td>
<td>@item.item_name</td>
<td>@item.item_description</td>
<td>
@if (item.Attachment != null)
{
if (item.Attachment.EndsWith("."))
{
<a href="@Url.Content(item.Attachment)" target="_blank"><img src="~/logo_email.jpg" height="50" width="53" /></a>
}
else
{
<img src="@Url.Content(item.Attachment)" height="200" width="220" />}
}
</td>
<td data-name="auditeeAttach" id="auditeeAttach">
@using (Html.BeginForm("ImageProven", "Auditee", FormMethod.Post, new { @class = "form-horizontal", role = "form", enctype = "multipart/form-data", id = "file1", name = "file1" }))
{
<!-- 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>
</tr>
}
}
</tbody>
</table>