Search code examples
c#asp.netasp.net-mvcdownloadhttphandler

asp.net mvc http handler based on specific folder path


We have a specific folder (i.e. c:\downloads\files) on the IIS server (asp.net mvc) that stores a bunch of zip files that the clients can download. Is this the proper scenario to use http handlers to handle such requests? Are there other techniques other than using handlers? Are there any links to tutorial to handlers based on specific file folder path? Thanks.


Solution

  • You could use MVC's FilePathResult to send your files to your clients:

    public ActionResult GetFile(string id)
    {
        // imaging you have a class which maps id with full file path
        // like c:\downloads\files\myfile.pdf
        string filePath=myFileManager.IsFileExist(id);
        if
        {
            // also you need to pass file's content type string
            // you could store this string when user uploads files
            return File(filePath, myFileManager.GetContentType(id),
                 Path.GetFileName(filePath));
        }
        return HttpNotFound();
    }
    

    Now you could call this action method with myContoller/GetFile/2 url.