locked
What is the best way to handle file that has been uploaded to the database in "edit" mode for CRUD? RRS feed

  • Question

  • User1901201124 posted

    I have a forum with a file upload and everything works file. I'm trying to get the Edit part to work and i'm wondering how I would be able to read that file from the database and output it to the file input textbox and if this is not best practice what is?

    I have a model like so:

            [NotMapped]
            [Display(Name = "ID File")]
            public HttpPostedFileBase IDFile { get; set; }
            public byte[] IDFileData { get; set; }
            public string IDFileName { get; set; }
            public string IDFIleType { get; set; }

    On my edit page I have the below file input box, so i'm wondering how I can read the file into it. I tried the below but it didn't do anything lol.

        <div class="col-md-4">
            @Html.LabelFor(model => model[0].IDFile, htmlAttributes: new { @class = "control-label" })
            <div class="input-group-sm input-group mb-2">
                @Html.TextBoxFor(model => model[0].IDFile, new { @type = "file", @data_val = "true", @data_val_required = "A File is required.", @class = "idfile1" })
            </div>
            @Html.ValidationMessageFor(model => model[0].IDFile, "", new { @class = "text-danger" })
        </div>

    Thanks

    Thursday, May 21, 2020 1:17 AM

Answers

  • User1686398519 posted

    Hi,  MVCNewbi3v

    According to the code you gave, “IDFile” does not have a corresponding column in the database, so when you read data from the database, the value of IDFile is empty.
    If you want to display the files you have uploaded on the page, you can refer to the example.

    Note:You need to add the field "ID", which needs to be used when you edit.

    File

    public class File
        {
            [NotMapped]
            [Display(Name = "ID File")]
            public HttpPostedFileBase IDFile { get; set; }
            public int ID { get; set; }
            public byte[] IDFileData { get; set; }
            public string IDFileName { get; set; }
            public string IDFIleType { get; set; }
        }

    Controller

    public ActionResult Index()
            {
                return View(db.Files.ToList());
            }
            public ActionResult Upload()
            {
                return View();
            }
            [HttpPost]
            public ActionResult Upload(File file)
            {
                try
                {
                        byte[] uploadedFile = new byte[file.IDFile.InputStream.Length];
                        file.IDFile.InputStream.Read(uploadedFile, 0, uploadedFile.Length);
                        file.IDFileData = uploadedFile;
                        file.IDFileName = file.IDFile.FileName;
                        file.IDFIleType = file.IDFile.ContentType;
                        db.Files.Add(file);
                        db.SaveChanges();
                }
                catch (Exception ex)
                {
                }
    
                return RedirectToAction("Index");
            }
            public ActionResult Edit(int ID)
            {
                File file = db.Files.Where(F => F.ID == ID).SingleOrDefault();
                return View(file);
            }
            [HttpPost]
            public ActionResult Edit(File file)
            {
                if (file.IDFile != null)
                {
                    try
                    {
                        byte[] uploadedFile = new byte[file.IDFile.InputStream.Length];
                        file.IDFile.InputStream.Read(uploadedFile, 0, uploadedFile.Length);
                        file.IDFileData = uploadedFile;
                        file.IDFileName = file.IDFile.FileName;
                        file.IDFIleType = file.IDFile.ContentType;
                        db.Files.AddOrUpdate(file);
                        db.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                    }
                }
                return RedirectToAction("Index");
            }
    
            public ActionResult RetrieveFile(int ID)
            {
                File file = db.Files.Where(F => F.ID == ID).SingleOrDefault();
                byte[] cover = file.IDFileData;
                if (cover != null)
                {
                    return this.File(cover, file.IDFIleType);
                }
                else
                {
                    return null;
                }
            }

    Index

    @Html.ActionLink("Upload", "Upload")
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.ID)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.IDFileName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.IDFIleType)
            </th>
            <th>
    
            </th>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.ID)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.IDFileName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.IDFIleType)
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.ID })
                </td>
            </tr>
    
        }
    </table>

    Edit

    <h2>Edit</h2>
    @using (Html.BeginForm("Edit", "File", FormMethod.Post, new { enctype = "multipart/form-data", @class = "form-horizontal", role = "form" }))
    {
        <div class="form-group">
            <div class="col-md-12">
                @Html.DisplayNameFor(m => m.IDFile)
                <br />
                <a class="download-file1" href="@Url.Action("RetrieveFile", "File", new {  ID = @Model.ID})" target="_blank">
                    @Model.IDFileName
                </a>
                @if (Model.IDFIleType.Contains("image"))
                {
                    <img src="@Url.Action("RetrieveFile", "File", new { ID = @Model.ID})" width="100" height="100" />
    
                }
                @Html.TextBoxFor(m => m.IDFile, new { type = "file", placeholder = Html.DisplayNameFor(m => m.IDFile), @class = "form-control" })
            </div>
        </div>
        
        <div class="form-group">
            <div class="col-md-12">
                <input type="submit" class="btn btn-danger" value="Edit" />
            </div>
        </div>
    }

    Upload

    @using (Html.BeginForm("Upload", "File", FormMethod.Post, new { enctype = "multipart/form-data", @class = "form-horizontal", role = "form" }))
    {
        <div class="form-group">
            <div class="col-md-12">
                @Html.TextBoxFor(m => m.IDFile, new { type = "file", placeholder = Html.DisplayNameFor(m => m.IDFile), @class = "form-control" })
            </div>
        </div>
    
        <div class="form-group">
            <div class="col-md-12">
                <input type="submit" class="btn btn-danger" value="Upload" />
            </div>
        </div>
    }

    Here is the result.

    Best Regards,

    YihuiSun
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 22, 2020 7:49 AM

All replies

  • User1686398519 posted

    Hi,  MVCNewbi3v

    According to the code you gave, “IDFile” does not have a corresponding column in the database, so when you read data from the database, the value of IDFile is empty.
    If you want to display the files you have uploaded on the page, you can refer to the example.

    Note:You need to add the field "ID", which needs to be used when you edit.

    File

    public class File
        {
            [NotMapped]
            [Display(Name = "ID File")]
            public HttpPostedFileBase IDFile { get; set; }
            public int ID { get; set; }
            public byte[] IDFileData { get; set; }
            public string IDFileName { get; set; }
            public string IDFIleType { get; set; }
        }

    Controller

    public ActionResult Index()
            {
                return View(db.Files.ToList());
            }
            public ActionResult Upload()
            {
                return View();
            }
            [HttpPost]
            public ActionResult Upload(File file)
            {
                try
                {
                        byte[] uploadedFile = new byte[file.IDFile.InputStream.Length];
                        file.IDFile.InputStream.Read(uploadedFile, 0, uploadedFile.Length);
                        file.IDFileData = uploadedFile;
                        file.IDFileName = file.IDFile.FileName;
                        file.IDFIleType = file.IDFile.ContentType;
                        db.Files.Add(file);
                        db.SaveChanges();
                }
                catch (Exception ex)
                {
                }
    
                return RedirectToAction("Index");
            }
            public ActionResult Edit(int ID)
            {
                File file = db.Files.Where(F => F.ID == ID).SingleOrDefault();
                return View(file);
            }
            [HttpPost]
            public ActionResult Edit(File file)
            {
                if (file.IDFile != null)
                {
                    try
                    {
                        byte[] uploadedFile = new byte[file.IDFile.InputStream.Length];
                        file.IDFile.InputStream.Read(uploadedFile, 0, uploadedFile.Length);
                        file.IDFileData = uploadedFile;
                        file.IDFileName = file.IDFile.FileName;
                        file.IDFIleType = file.IDFile.ContentType;
                        db.Files.AddOrUpdate(file);
                        db.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                    }
                }
                return RedirectToAction("Index");
            }
    
            public ActionResult RetrieveFile(int ID)
            {
                File file = db.Files.Where(F => F.ID == ID).SingleOrDefault();
                byte[] cover = file.IDFileData;
                if (cover != null)
                {
                    return this.File(cover, file.IDFIleType);
                }
                else
                {
                    return null;
                }
            }

    Index

    @Html.ActionLink("Upload", "Upload")
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.ID)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.IDFileName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.IDFIleType)
            </th>
            <th>
    
            </th>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.ID)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.IDFileName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.IDFIleType)
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.ID })
                </td>
            </tr>
    
        }
    </table>

    Edit

    <h2>Edit</h2>
    @using (Html.BeginForm("Edit", "File", FormMethod.Post, new { enctype = "multipart/form-data", @class = "form-horizontal", role = "form" }))
    {
        <div class="form-group">
            <div class="col-md-12">
                @Html.DisplayNameFor(m => m.IDFile)
                <br />
                <a class="download-file1" href="@Url.Action("RetrieveFile", "File", new {  ID = @Model.ID})" target="_blank">
                    @Model.IDFileName
                </a>
                @if (Model.IDFIleType.Contains("image"))
                {
                    <img src="@Url.Action("RetrieveFile", "File", new { ID = @Model.ID})" width="100" height="100" />
    
                }
                @Html.TextBoxFor(m => m.IDFile, new { type = "file", placeholder = Html.DisplayNameFor(m => m.IDFile), @class = "form-control" })
            </div>
        </div>
        
        <div class="form-group">
            <div class="col-md-12">
                <input type="submit" class="btn btn-danger" value="Edit" />
            </div>
        </div>
    }

    Upload

    @using (Html.BeginForm("Upload", "File", FormMethod.Post, new { enctype = "multipart/form-data", @class = "form-horizontal", role = "form" }))
    {
        <div class="form-group">
            <div class="col-md-12">
                @Html.TextBoxFor(m => m.IDFile, new { type = "file", placeholder = Html.DisplayNameFor(m => m.IDFile), @class = "form-control" })
            </div>
        </div>
    
        <div class="form-group">
            <div class="col-md-12">
                <input type="submit" class="btn btn-danger" value="Upload" />
            </div>
        </div>
    }

    Here is the result.

    Best Regards,

    YihuiSun
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 22, 2020 7:49 AM
  • User1901201124 posted

    Thank you, that is the way I thought I would take. Just wasn't sure if this was the best practice or if there are any other ways people are doing it.

    Friday, May 22, 2020 12:44 PM