[英]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.
http:/localhost/Images/PDF/150763-3.pdf不是有效的虚拟路径。
If I post this URL http:/localhost/Images/PDF/150763-3.pdf
in my browser, the file is opened. 如果我在浏览器中发布此URL
http:/localhost/Images/PDF/150763-3.pdf
,则会打开该文件。 How can I download this file? 如何下载此文件?
Platform MVC 4, IIS 8. 平台MVC 4,IIS 8。
It should be http://localhost/Images/PDF/150763-3.pdf
(instead of http:/) 它应该是
http://localhost/Images/PDF/150763-3.pdf
(而不是http:/)
Chrome will change http:/ to http:// but your program will not. Chrome会将http:/更改为http://,但您的程序不会更改。
I think I misread your question. 我想我误解了你的问题。
Try (fixed from comment) 试试(从评论中修复)
return File(FullfilePhysicalPath, "Application/pdf", DateTime.Now.ToLongTimeString()+".pdf");
If you want use routes url in format: "{controller}/{action}/{id}": 如果要使用以下格式的路由网址:“ {controller} / {action} / {id}”:
There is class RouteConfig defined ~/App_Start/RouteConfig.cs in MVC 4 You have ImageController and PDF action and 150763-3.pdf is the parameter id. 在MVC 4中有一个定义为〜/ App_Start / RouteConfig.cs的RouteConfig类。您具有ImageController和PDF操作,并且150763-3.pdf是参数id。
http://localhost/Images/PDF/150763-3.pdf http://localhost/Images/PDF/150763-3.pdf
Solution is very simple: 解决方法很简单:
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.