[英]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.