locked
Downloading a file using C# in an ASP.NET MCV project RRS feed

  • Question

  • first time posting so if I make any mistakes and not follow the guidelines please correct me.

    So I working on an MCV project on my own. Creating a server which will hand out files to registered users. I have some HTML experience so I tried using that to download the file but couldn't. I get the browser to try to download the file but immediately it says the file cant be found or doesnt exist. I have a feeling its because IIS is not allowing the client to access the file system. Of course its just a guess. Im using Microsoft Visual Studio 2015 as my IDE if that makes a difference. Heres the code.

    @model MainApplication.Models.IndexViewModel
    @{
        ViewBag.Title = "Download";
    }
    
    <h2>@ViewBag.Title</h2>
    
    <p class="text-success">@ViewBag.Message</p>
    
    <div>
    
        <ul>
            @{
                string dirPath = "\\Users\\hippiewho\\Documents\\Visual Studio 2015\\Projects\\Web Application\\MainApplication\\Views\\Download";
                string dirDownPath = "..\\Download\\";
                Response.ClearContent();
                Response.Clear();
                System.Web.MimeMapping.GetMimeMapping("w3logo.jpg");
            }
            @{
                List<string> dirs = new List<string>(Directory.EnumerateFiles(dirPath));
                foreach (var dir in dirs)
                {
                    <li><a class="btn-block" href="@dirDownPath@dir.Substring(dir.LastIndexOf("\\") + 1)" download >@dir.Substring(dir.LastIndexOf("\\") + 1)</a></li>
                }
            }
        </ul>
    </div>
    
    The controller doesnt have much in it except the view() function and the ViewBag title which is download.

    • Moved by Kristin Xie Tuesday, October 13, 2015 5:46 AM
    Monday, October 12, 2015 6:34 PM

Answers

  • ASP.NEt question belong into a seperate forum:

    http://forums.asp.net/

    But I do still ahve some experience. In order to allow downloading a file from a server you must either:

    - Place that file into a subdirectory of the content directory and give the client browser the proper path. Security management on this is tricky, so best leave it for stuff that is public anyway.

    - Or use a HTTP Handler. This is very often used if the file is stored in/with a Database or the file has to be created in memory. It also allows you complete control over who get's teh file and who not.
    Reading up on the 3 approaches to store a larger file within a DB might help:
    An Introduction to SQL Server FileStream

    Keep in mind that Webservers traditionally run under very limited rights (as they are exceptionalyl vulnerable to being hacked).
    Reading it's programm and the content directory is often the only thing they are allowed to do. Even having a directory with write rights below the content directory (for temporary files) is a rare occurence.

    • Edited by Christopher84 Monday, October 12, 2015 7:47 PM
    • Proposed as answer by Just Karl Tuesday, October 13, 2015 9:53 PM
    • Marked as answer by Just Karl Friday, October 23, 2015 10:46 PM
    Monday, October 12, 2015 7:47 PM