I want to upload Images to a database. The database contains url images and images uploads to folder in file system. I have the following tables in the database,
Furniture
MainFileDetails
(1-1 relationship withFurniture
) where store the main imageFileDetails
(1-Many relationship withFurniture
) where we store other images associated withFurniture
.
Here is my code:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Furniture furniture , HttpPostedFileBase fileTwo, HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
for (int i = 0; i < Request.Files.Count; i++)
{
fileTwo = Request.Files[i];
if (fileTwo != null && fileTwo.ContentLength > 0)
{
var fileNameTwo = Path.GetFileName(fileTwo.FileName);
MainFileDetails mainFileDetail = new MainFileDetails()
{
FileName = fileNameTwo,
Extension = Path.GetExtension(fileNameTwo),
Id = Guid.NewGuid(),
FurnitureId = furniture.FurnitureId
};
var pathMain = Path.Combine(Server.MapPath("~/Upload/MainPage/"), mainFileDetail.Id + mainFileDetail.Extension);
fileTwo.SaveAs(pathMain);
db.Entry(mainFileDetail).State = EntityState.Modified;
db.SaveChanges();
FileDetail fileDetail = new FileDetail()
{
NameFile = fileNameTwo, //or mainFileDetail.FileName
Extension = Path.GetExtension(fileNameTwo), //or mainFileDetail.Extension
Id = Guid.NewGuid(),
FurnitureId = furniture.FurnitureId //or mainFileDetail. FurnitureId
};
var path = Path.Combine(Server.MapPath("~/Upload/"), fileDetail.Id + fileDetail.Extension);
file.SaveAs(path);
db.Entry(fileDetail).State = EntityState.Added;
}
}
db.Entry(furniture).State = EntityState.Modified;
db.SaveChanges();
TempData["message"] = string.Format("Changes in \"{0}\" has been saved", furniture.Name);
return RedirectToAction("Index");
}
ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "Name", furniture.CategoryId);
return View(furniture);
}
My View:
@model FurnitureStore.Entities.Furniture
....
@using (Html.BeginForm("Edit", "Furnitures", FormMethod.Post, new { enctype = "multipart/form-data"}))
{
....
@Html.HiddenFor(model => model.FurnitureId)
@Html.LabelFor(model => model.Name)
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
.... // more inputs for properties of the model
@Html.LabelFor(model => model.CategoryId, "Category")
@Html.DropDownList("CategoryId", null)
@Html.ValidationMessageFor(model => model.CategoryId, "", new { @class =
// Main file
<input type="file" name="fileTwo" />
@if(Model.MainFileDetails.Id == null)
{
<div class="form-control-static">No image</div>
}
else
{
<img src="~/Upload/MainPage/@(Model.MainFileDetails.Id + Model.MainFileDetails.Extension)" width="240" height="240" />
}
// Other files
<input type="file" name="file" multiple="multiple" />
<ul class="attachment">
@foreach (var item in Model.FileDetails)
{
<li style="list-style-type:none; display:inline;">
<img src="~/Upload/@(item.Id + item.Extension)" />
<a href="javascript:void(0);" data-id="@item.Id" class="deleteItem">X</a>
</li>
}
</ul>
<input type="submit" value="Save" class="btn btn-primary" />
}
But I have a problem. All photo just uploads to the Upload/MainPage
folder. I want to upload photo separately to MainPage in one table and separately to gallery (other table).
How can I fix my code, because last uploaded photo in gallery table replaces photo in MainPage?