1

I have been reading and working around downloading and uploading files to database! I just made upload work, so now i save my files in Binary in the database! Also i display the name in the View :

@model IEnumerable<RH_Files>

<div class="tableContainer">
    <table class="table table-striped">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(p => p.Name)
                </th>
            </tr>
        </thead>
        <tbody >
            @foreach (var item in Model)
            {
              <tr>
                  <td>
                      @Html.DisplayFor(model => item.Name)
                  </td>
              </tr>
            }
        </tbody>
    </table>
</div>

as you can see : enter image description here

my model:

 public class RH_Files
    {
        [Key]
        public int id { get; set; }
        public string Name { get; set; }
        public string ContentType { get; set; }
        public Byte[] Data { get; set; }
    }

Now what i need, is to have a download button for each item, that will trigger a c# action that downloads the file selected...

Any help is appreciated!!

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Ricardo Figueiredo
  • 206
  • 1
  • 5
  • 14

1 Answers1

2

in your controller:

public FileResult Download(int id)
{
      var fileToRetrieve = db.GetFile(id);
      return File(fileToRetrieve.Data, fileToRetrieve.ContentType);
}
Lasse Edsvik
  • 9,070
  • 16
  • 73
  • 109