繁体   English   中英

从虚拟目录下载文件

[英]Downloading a File From Virtual Directory

我在这里做错了,但无法解决。 我有一个虚拟目录和一个文件,我想下载该文件。

我的代码:

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" });
    }
}

我收到一个错误:

http:/localhost/Images/PDF/150763-3.pdf不是有效的虚拟路径。

如果我在浏览器中发布此URL http:/localhost/Images/PDF/150763-3.pdf ,则会打开该文件。 如何下载此文件?

平台MVC 4,IIS 8。

它应该是http://localhost/Images/PDF/150763-3.pdf (而不是http:/)

Chrome会将http:/更改为http://,但您的程序不会更改。


我想我误解了你的问题。

试试(从评论中修复)

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

如果要使用以下格式的路由网址:“ {controller} / {action} / {id}”:

在MVC 4中有一个定义为〜/ App_Start / RouteConfig.cs的RouteConfig类。您具有ImageController和PDF操作,并且150763-3.pdf是参数id。

http://localhost/Images/PDF/150763-3.pdf

解决方法很简单:

public class ImagesController : Controller
    {
        [ActionName("PDF")]
        public ActionResult DownloadFile(string id)
        {
            if (id == null)
                return new HttpNotFoundResult();

            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
            string FileName = id;

            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", FileName);
            }
            else
            {
                return Json(new { Success = "false" });
            }

        }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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