M
M
Michael2017-02-13 15:51:50
Database
Michael, 2017-02-13 15:51:50

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);

        }

and partial code from the view:
<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>

What am I doing here, I am uploading images for the gallery, but I want to display one of the photos for the gallery on the main page of products on my website, so I check for the field in the database, is it false or true, and here is the question: let's say we delete the photo from the gallery and it accordingly deletes the photo on the main page, since one thing is all that you can think of so that when you delete a photo from the gallery, the photo somehow remains on the main page, or what about creating a new table in the database? Please give me some ideas, thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kovalsky, 2017-02-14
@dmitryKovalskiy

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 question

Ask a Question

731 491 924 answers to any question