user2978444
user2978444

Reputation: 193

Download file from link inside my webpage

I have Webpage with table of objects.

One of my object properties is the file path, this file is locate in the same network. What i want to do is wrap this file path under link (for example Download) and after the user will click on this link the file will download into the user machine.

so inside my table:

@foreach (var item in Model)
        {    
        <tr>
            <th width ="150"><p><b><a href="default.asp" target="_blank">Download</a></b></p></th>
            <td width="1000">@item.fileName</td>
            <td width="50">@item.fileSize</td>
            <td bgcolor="#cccccc">@item.date<td>
        </tr>
    }
    </table>

I created this download link:

<th width ="150"><p><b><a href="default.asp" target="_blank">Download</a></b></p></th>

I want this download link to wrap my file path and click on thie link will lean to my controller:

public FileResult Download(string file)
{
    byte[] fileBytes = System.IO.File.ReadAllBytes(file);
}

What i need to add to my code in order to acheive that ?

Upvotes: 19

Views: 33132

Answers (3)

Hamid
Hamid

Reputation: 1563

This example works fine for me:

public ActionResult DownloadFile(string file="")
        {

            file = HostingEnvironment.MapPath("~"+file);

            string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            var fileName = Path.GetFileName(file);
            return File(file, contentType,fileName);    

        }

View:

< script >
function SaveImg()
{
    var fileName = "/upload/orders/19_1_0.png";
    window.location = "/basket/DownloadFile/?file=" + fileName;
}
< /script >
<img class="modal-content" id="modalImage" src="/upload/orders/19_1_0.png" onClick="SaveImg()">

Upvotes: 0

mehmet mecek
mehmet mecek

Reputation: 2685

Return FileContentResult from your action.

public FileResult Download(string file)
{
    byte[] fileBytes = System.IO.File.ReadAllBytes(file);
    var response = new FileContentResult(fileBytes, "application/octet-stream");
    response.FileDownloadName = "loremIpsum.pdf";
    return response;
}

And the download link,

<a href="controllerName/[email protected]" target="_blank">Download</a>

This link will make a get request to your Download action with parameter fileName.

EDIT: for not found files you can,

public ActionResult Download(string file)
{
    if (!System.IO.File.Exists(file))
    {
        return HttpNotFound();
    }

    var fileBytes = System.IO.File.ReadAllBytes(file);
    var response = new FileContentResult(fileBytes, "application/octet-stream")
    {
        FileDownloadName = "loremIpsum.pdf"
    };
    return response;
}

Upvotes: 33

Rizvi Sarwar
Rizvi Sarwar

Reputation: 26

In the view, write:

<a href="/ControllerClassName/DownloadFile?file=default.asp" target="_blank">Download</a>

In the controller, write:

public FileResult DownloadFile(string file)
    {
        string filename = string.Empty;
        Stream stream = ReturnFileStream(file, out filename); //here a backend method returns Stream
        return File(stream, "application/force-download", filename);
    }

Upvotes: 0

Related Questions