Search code examples
c#asp.net-mvc-4downloadvirtual-directoryiis-8

Downloading a File From Virtual Directory


I am doing something wrong here, but not able to figure it out. I have a virtual directory and a files inside it and I want to download the file.

My code:

public ActionResult DownloadFile()
{
    string FileName = Request.Params["IMS_FILE_NAME"];
    string FullFileLogicalPath = Path.Combine(ConfigurationManager.AppSettings["VIRTUAL_DIR_PATH"], FileName);
    string FullfilePhysicalPath = Path.Combine(ConfigurationManager.AppSettings["PHYSICAL_DIR_PATH"], FileName);
    if (System.IO.File.Exists(FullfilePhysicalPath))
    {
        return File( FullFileLogicalPath , "Application/pdf", DateTime.Now.ToLongTimeString());
    }
    else
    {
        return Json(new { Success = "false" });
    }
}

I am getting an error:

http:/localhost/Images/PDF/150763-3.pdf is not a valid virtual path.

If I post this URL http:/localhost/Images/PDF/150763-3.pdf in my browser, the file is opened. How can I download this file?

Platform MVC 4, IIS 8.


Solution

  • It should be http://localhost/Images/PDF/150763-3.pdf (instead of http:/)

    Chrome will change http:/ to http:// but your program will not.


    I think I misread your question.

    Try (fixed from comment)

    return File(FullfilePhysicalPath, "Application/pdf", DateTime.Now.ToLongTimeString()+".pdf");