简体   繁体   中英

asp.net mvc http handler based on specific folder path

We have a specific folder (ie 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM