Download file from link inside my webpage
Asked Answered
S

3

19

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 ?

Salvidor answered 14/11, 2013 at 10:50 Comment(0)
P
33

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;
}
Prostitute answered 14/11, 2013 at 10:59 Comment(12)
And how to ensure that this controller mothed received my file path ? what i need to add in my view ?Salvidor
You already have a link that can make a GET request just put your controllername/actionName to the href attributeProstitute
I can reach now my controller method but the file is null: <th width ="150"><p><b><a href="Download" target="_blank">Download</a></b></p></th>Salvidor
Put fileName with query string after your href link. (href="[email protected]")Prostitute
Ok its working and downoad but it download with other name and no extension, how can i save the original file name and extension ?Salvidor
I modified the answer with providing file download name also. If you are downloading image, sound or video files you can change the content type accordingly. (en.wikipedia.org/wiki/Internet_media_type#Type_application)Prostitute
What does loremIpsum.pdf mean ?Salvidor
It is your file name with extention. lorem ipsum is dummy text en.lipsum.comProstitute
"application.octet-stream" exists as a constant in the framework: System.Net.Mime.MediaTypeNames.Application.Octet Check out the MSDNBurdette
@mecek How would you go about providing validation in your solution? ie. Returning an error if the file doesn't exist.Amoeboid
@Amoeboid I made an edit on the answer, basically FileResut is also ActionResult so you can return 404 after checking file existence..Prostitute
Why is target="_blank" necessary? I removed it and the file downloaded fine for Chrome without opening a new tab.Ultun
I
0

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);
    }
Issuable answered 3/2, 2015 at 15:39 Comment(0)
K
0

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()">
Kowtow answered 17/1, 2017 at 16:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.