Answer the question
In order to leave comments, you need to log in
How to deal with multi-uploading images?
Hello, I am uploading several images to the file system (more precisely, the pictures themselves are loaded into a folder, and information about the pictures is stored in the database). Here is my my method in controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Furniture model)
{
if (ModelState.IsValid)
{
//New Files
for (int i = 0; i < Request.Files.Count; i++)
{
var file = Request.Files[i];
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
FileDetail fileDetail = new FileDetail()
{
NameFile = fileName,
Extension = Path.GetExtension(fileName),
Id = Guid.NewGuid(),
FurnitureId = model.FurnitureId,
};
var path = Path.Combine(Server.MapPath("~/Upload/"), fileDetail.Id + fileDetail.Extension);
file.SaveAs(path);
db.Entry(fileDetail).State = EntityState.Added;
}
}
db.Entry(model).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(model);
}
<p><b>Main Files</b></p>
<div class="form-group">
@foreach (var item in Model.FileDetails)
{
if (item.IsMainImage == true)
{
<img src="~/Upload/@(item.Id + item.Extension)" />
}
}
</div>
<div class="form-group">
<input type="file" name="fileTwo" 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>
</div>
Answer the question
In order to leave comments, you need to log in
Do you want to delete the photo without deleting the photo? In my opinion, a potential client, when performing the action of deleting a photo, assumes that it will be deleted, and will not remain in place. Example - I delete the picture, but it remains on the main page. But it's all business logic thinking, and if you have a good excuse to do so, then do it.
What can be done? Set field isDeleted. On the main page, ignore the value of this field and display the picture, and where you want - read and do not show if it is true
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question