ASP.Net MVC displaying stored .zip files and allowing downloading from database

FlameDra

I have an Assignment model which stores a StudentName and a .zip file (amongst other items) to a database. I have tested out my Create method, and it manages to create the Assignment with the corresponding File selected and uploads it to the database for storage. That portion is working correctly. I am having trouble displaying the stored file in my View as a downloadable link.

Here is how the Database is set up.

Assignments Table:

enter image description here

Files Table:

enter image description here

The File in the Files table associates a relationship with the Assignment through a foreign key relationship where AssignmentId is stored in each File entry in the database.

When I run it and create an Assignment and look at the table values in the Database, I can see that the file is being uploaded and stored and the relationships created. Here is how my models look.

Assignment Model:

namespace ProjectVoting.Models
{
    public class Assignment
    {
        [Key]
        public int Id { get; set; }
        public string StudentName { get; set; }
        public int Votes { get; set; }

        public virtual ICollection<File> Files { get; set; }
    }
}

File Model:

namespace ProjectVoting.Models
{
    public class File
    {
        public int FileId { get; set; }
        [StringLength(255)]
        public string FileName { get; set; }
        [StringLength(100)]
        public string ContentType { get; set; }
        public byte[] Content { get; set; }
        public FileType FileType { get; set; }
        public int AssignmentId { get; set; }
        public virtual Assignment Assignment { get; set; }
    }
}

FileType Class:

namespace ProjectVoting.Models
{
    public enum FileType
    {
        Project = 1, ZipFile
    }
}

In the Index view of the Assignment model, I am trying to display a list of the StudentName, File, and the Edit/Details/Delete links. Here is how I have the view set up.

Assignment/Index View:

@model IEnumerable<ProjectVoting.Models.Assignment>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.StudentName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Votes)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.StudentName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Votes)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Files)
        </td>

        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}

</table>

Note, I am using @Html.DisplayFor(modelItem => item.Files) to try to display a download link to the file, from the database. However, when I get to the index page, it is shown as this:

enter image description here

What is the proper way to display the downloadable link to the File from the database in the table?

Victor Leontyev

You need to create additional action method inside your controller for downloading the file.

In you case, in your View you need to replace

@Html.DisplayFor(modelItem => item.Files)

With loop with downloading link for each file

@foreach(var file in item.Files)
{
    @Html.ActionLink("Download "+file.FileName, "Download", new { id=file.FileId })
}

And in your controller add new action

public ActionResult Download(int id)
{
    //get File by fileId
    var file = ........
    byte[] contents = file.Content;
    return File(contents, file.ContentType)
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ASP.NET Handler (ashx) vs MVC Controller Action for downloading files

Downloading of zip file through ASP.NET MVC using DotNetZip

populate radiobuttonlist from database table in asp.net mvc

Asp.Net Mvc 5 image not displaying

Inject data from database to a class with ASP.NET MVC Core

Bootstrap slider in ASP.NET MVC from SQL Server database

Uploading and displaying image in ASP.NET MVC

ASP.NET MVC Displaying Data in the view from a model?

Displaying a JSON in a table ASP.Net MVC

Reading information from database by controller in ASP.NET Core MVC

Problems with files downloading from Google Drive Api using asp.net mvc

Is it correct way of displaying Image from .NET Core API in ASP.NET MVC View?

How to generate & render url from database ASP.Net MVC

Generating seed code from existing database in ASP.NET MVC

Razor asp.net webpages - displaying all rows from a database

displaying last record in the database asp.net mvc

ASP.NET MVC not displaying image

Allowing POST method to an HTML page in ASP.NET MVC

ASP.NET MVC 5 TryUpdateModel not allowing & in string

ASP.NET MVC: Displaying multiple Charts from Model

Why are files are different when downloading from an ASP.NET (AJAX download with Blob)

Downloading FIles on Asp.net

ASP.NET MVC Retrieve Data from Database to DropDownList

ASP.NET MVC Validation errors not displaying

select multiple entities from database in asp.net mvc

How to download files from a database, and get them as image in Asp.net core MVC?

Issue downloading Files from Web App - ASP.NET Web Form Application

Downloading zip files with python

Translate result of DbDataReader to database model in ASP.NET MVC 4, coming from a stored procedure using ADO.NET