Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download file from folder in view that already contains table

I have a pre-existing view/controller that contains a table, and I am already passing an argument to the view from the ActionResult. Is it possible to have file downloads on the same view as the table, all examples I have found so far create a separate ActionResult and view which I am trying to avoid. Is there a way to create a download link of folder files with a ViewBag or something instead so it call all be within the one view?

This the is link I was following for what I am trying to do if my explanation does not make sense.

Download File in MVC4

Here is my existing controller and view

Controller

public ActionResult P1A1Mark()
    {


        List<MarkModel> query = (from row in db.submits
                                     where row.assignment_no.Equals("1") && row.group_no == 1
                                     group row by new { row.assignment_no, row.student_no, row.student.firstname, row.student.surname } into g
                                     select new MarkModel
                                     {
                                         student_no = g.Key.student_no,
                                         student_surname = g.Key.surname,
                                         student_firstname = g.Key.firstname

                                     }
                                        ).ToList();

        return View(query);
    }

View

@model IList<MvcApplication2.Models.MarkModel>

@{
    ViewBag.Title = "P1A1Mark";
}

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<h2>Mark Student Assignments</h2>
    

<table class="table">
    <tr>
        <th>
            Student Number 
            @*@Html.DisplayNameFor(model => model.student_no)*@
        </th>
        <th>
            Surname
            @*@Html.DisplayNameFor(model => model.student_surname)*@
        </th>
        <th>
            Firstname
            @*@Html.DisplayNameFor(model => model.student_firstname)*@
        </th>
          <th>
            Group
        </th>
         <th>
             Submitted
            @*@Html.DisplayNameFor(model => model.submitted)*@
        </th>
        <th>
            Result
            @*@Html.DisplayNameFor(model => model.result)*@
        </th>

        <th></th>
    </tr>

@*@foreach (var item in Model) {*@
@for  (var i = 0; i < Model.Count; i++)  {
    <tr>
        <td>
            @*@Html.DisplayFor(modelItem => item.student_no)*@
            <div style="width:150px;float:left;">
            @Html.DisplayFor(modelItem => modelItem[i].student_no)
                </div>
        </td>
        <td>
            @*@Html.DisplayFor(modelItem => item.student_surname)*@
            <div style="width:100px;float:left;">
            @Html.DisplayFor(modelItem => modelItem[i].student_surname)
                </div>
        </td>
        <td>
            @*@Html.DisplayFor(modelItem => item.student_firstname)*@
            <div style="width:100px;float:left;">
            @Html.DisplayFor(modelItem => modelItem[i].student_firstname)
                </div>
        </td>
        <td>
            <div style="width:100px;float:left;">
            @Html.DisplayFor(modelItem => modelItem[i].group_no)
                @Html.HiddenFor(modelItem => modelItem[i].student_no)
                </div>
        </td>
        <td>
            @*@Html.DisplayFor(modelItem => item.submitted)*@
            <div style="width:100px;float:left;">
            @Html.CheckBoxFor(modelItem => modelItem[i].submitted)
            @Html.HiddenFor(modelItem => modelItem[i].student_no)
                </div>
        </td>
        <td>
            @*@Html.DisplayFor(modelItem => item.result)*@
            @*@Html.TextBoxFor(modelItem => item.result, new {style = "width: 35px;"})*@
            <div style="width:100px;float:left;">
            @Html.TextBoxFor(modelItem => modelItem[i].result, new {style = "width: 35px;"})%
                @Html.HiddenFor(modelItem => modelItem[i].student_no)
                </div>
        </td>

    </tr>
}

</table>
    <br />
    <br />
    <button type="submit">Submit Marks</button>
}
like image 646
cg91 Avatar asked Dec 05 '25 03:12

cg91


1 Answers

If your model contains enough data to build a link to download the file then you can simply add those links to your view. I don't know your model or the link format you need, so this is just an example. But it might look something like this:

@Html.ActionLink("Click here to download", "DownloadFile", new { fileID = Model.FileID })

Or even just manually like this:

<a href="/Files/[email protected]">Click here to download</a>

However you build the link to your file, you can do it in the view if you have the data to create that link.


One thing you can't do (and it sounds like this is what you want) is send the file itself with the view. HTTP requests and responses are very simple things. The response which contains this view is an HTML response, not a file response. A separate request would need to be made to get the file response. The links are what would facilitate those requests.

So essentially the order of operations would be:

  1. User requests page
  2. View is sent to user containing links to files
  3. User requests File 123
  4. File 123 is sent to user
  5. User requests File 234
  6. File 234 is sent to user
  7. etc.

You can potentially send multiple files in a single response if you use code to add them all to a .zip file and send that to the user. So if the user is going to download 5 files then they can instead download 1 .zip file which contains the 5. That's a bit more involved, but not entirely uncommon.

like image 120
David Avatar answered Dec 09 '25 00:12

David



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!