locked
downloading a file from sql server in asp mvc RRS feed

Answers

  • User-893317190 posted

    Hi tmatiwure ,

    In you link, you have  recorded  the path of your  model's property  ImagePath , all you need to do is to render an a link, and  create another action in your controller to return the file.

    Below is my sample code. Here download is the action to download file.

     public class DemoController : Controller
        {
            // GET: Demo
            public ActionResult ShowImagePath()
            {
                ImageDbcontext imageDbcontext = new ImageDbcontext();
                
                return View(imageDbcontext.images.ToList());
            }
            public ActionResult Download(string path)
            {
                return File(path, "application/octet-stream",path);
            }
        }

    In my view. @Html.ActionLink("download","download", new { path =item.path }) creates an a link to pass the file path to the download action , then the action could return corresponding image using File method.

    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.path)
            </td>
            <td>
               @Html.ActionLink("download","download", new { path =item.path })
            </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>
    }

    The result.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 13, 2019 2:32 AM

All replies

  • User-893317190 posted

    Hi tmatiwure ,

    In you link, you have  recorded  the path of your  model's property  ImagePath , all you need to do is to render an a link, and  create another action in your controller to return the file.

    Below is my sample code. Here download is the action to download file.

     public class DemoController : Controller
        {
            // GET: Demo
            public ActionResult ShowImagePath()
            {
                ImageDbcontext imageDbcontext = new ImageDbcontext();
                
                return View(imageDbcontext.images.ToList());
            }
            public ActionResult Download(string path)
            {
                return File(path, "application/octet-stream",path);
            }
        }

    In my view. @Html.ActionLink("download","download", new { path =item.path }) creates an a link to pass the file path to the download action , then the action could return corresponding image using File method.

    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.path)
            </td>
            <td>
               @Html.ActionLink("download","download", new { path =item.path })
            </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>
    }

    The result.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 13, 2019 2:32 AM
  • User-164452226 posted

    Got it, thank you so much.

    Tuesday, May 14, 2019 2:57 PM