-1

I have a folder with a .mp4 file that I want to be downloadable. I have the following code:

The PartialView:

    @model List<string>

<div>
    <div class="messeDiv">
        <h2>Downloads</h2>
        <br /><br />

        @for (var i = 0; i < Model.Count; ++i)
        {
            <div>           
                @Model[i].ToString()             
            </div>

            <div>
                 @Html.ActionLink("Download file", "DownloadFile", "FileDownload")                
            </div>
        }

    </div>
</div>

And the controller:

[Authorize]
    public ActionResult DownloadFile()
    {
        string userMail = User.Identity.GetUserName();
        string role = logic.GetUserRole(userMail);

        if (!role.ToLower().Equals("model"))
        {
            return PartialView("_DenyAcces");
        }

        var dir = new DirectoryInfo(Server.MapPath("~/Videos/"));
        FileInfo[] fileNames = dir.GetFiles("*.mp4");
        List<string> items = new List<string>();

        foreach (var file in fileNames)
        {
            items.Add(file.Name);
        }

        return PartialView("_AllFiles", items);
    }

I want to make it so that if I click on the: @Html.ActionLink("Download file", "DownloadFile", "FileDownload") link, then I should be asked if I want to download the file, just like what happens in a normal downloadable file. I am very new to asp.net mvc, and right now it is just out of control for me. I have tried finding different examples, and from those made the code I have so far. What more do I need to do to achieve what I want?

Right now when I click on the link I am taken to the URL: http://localhost:3918/FileDownload/DownloadFile and nothing happens.

user1960836
  • 1,732
  • 7
  • 27
  • 47
  • 1
    possible duplicate of [ASP.NET MVC: Retrieving an .mp3 file and returning it to the user](http://stackoverflow.com/questions/22845678/asp-net-mvc-retrieving-an-mp3-file-and-returning-it-to-the-user) – markpsmith Mar 23 '15 at 17:37
  • I saw that post, I didn't find comfort in it unfortunately – user1960836 Mar 23 '15 at 17:39
  • Really? To me, it sound exactly like what you're trying to do. What is missing from that answer exactly? – markpsmith Mar 23 '15 at 17:44
  • In lack of skills, I kept getting an exception with that approach. I don't recall the exact message, but I was using ReadAllBytes instead of ReadBytes, and no matter what I changed, I couldn't get rid of it. So I changed approach. – user1960836 Mar 23 '15 at 17:55

1 Answers1

0

a) To have a confirmation message ask the user you can use this.

$(document).ready(function(){
    $('#downloadFileLink').click(function(){ 
       if(!window.confirm("Download file?"))
         return false;
    });
});

b) To return a file you need an action returning FileResult like below. So I assume your partial view has this download link with the path in the href.

public FileResult File(string path)
{
    if (!string.IsNullOrEmpty(path))
    {
            var fileName = Server.MapPath(path);
            var file = new FileInfo(fileName);
            if (file.Exists)
            {
                return File(fileName, "video/mpeg");
            }
    }

    //file is empty, so return null
    return null;
}

Note: I have not tested this but I think it should work just fine.

Judge Bread
  • 501
  • 1
  • 4
  • 13
  • Can one return a View/PartialView in a FileResult instead of null? Returning null results in a blank page where the user doesn't know what's going on – user1960836 Mar 23 '15 at 17:59
  • Why are you trying to return a view from a FileResult? The FileResult is to return a file. ["Represents a base class that is used to send binary file content to the response."](https://msdn.microsoft.com/en-us/library/system.web.mvc.fileresult%28v=vs.118%29.aspx) What do you want to do exactly? – Judge Bread Mar 23 '15 at 18:02
  • How would you call that method from an Action method in a controller that returns a View? If file does not exist, null is returned, but the Action method must return a View/PartialView – user1960836 Mar 23 '15 at 18:08
  • 1
    These are some other links you might be interested in. [Large Files Download](http://www.codeproject.com/Tips/842832/How-to-Download-Large-Files-from-ASP-NET-Web-Forms) and [Upload/Download files](http://www.mikesdotnetting.com/article/125/asp-net-mvc-uploading-and-downloading-files). As to your above comment/question I believe [this answer](http://stackoverflow.com/a/11434440/1453651) would be the best approach to handling the null situation. – Judge Bread Mar 23 '15 at 18:15