在Web应用程序中的服务器上提供静态文件下载 [英] Provide static file download at server in web application
问题描述
我正在开发MVC 4 Web应用程序.在一页上,我提供了一个指向应用程序目录中文件的锚链接.相同的代码是-
@Html.Action("Download_Static_File", "Charge_Entry", new { File_Path = "../../Content/Templates/Pt_Data/Pt_Data.xls", File_Name = "Pt_Data_Template", value = "Download template" });
我的动机是应该在点击时下载文件.
但是,当我单击链接时,会出现类似
的错误找不到路径'C:\ Program Files \ Common Files \ Microsoft Shared \ Content \ Templates \ Pt_Data \ Pt_Data.xls'的一部分.'
我也尝试过
System.Web.HttpContext.Current.Server.MapPath
出现此错误:
使用自定义TextWriter时,OutputStream不可用.
被调用的动作方法是:
public FileResult Download_Static_File(string File_Path,string File_Name)
{
byte[] fileBytes = System.IO.File.ReadAllBytes(File_Path);
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, File_Name);
}
这是正确的方法吗?任何帮助将不胜感激.
我还引用了此链接
您的锚点似乎指向名为Download_Static_File
的控制器动作(不幸的是,您没有显示)并传递了名为File_Path
的参数./p>
您在视图中使用的@Html.Action
助手正在尝试将指定的操作作为子操作执行.您可能会发现以下博客文章对描述子操作很有用: http ://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx/
我想您要实现的目标是在视图中生成一个指向静态文件的锚,该锚可以由用户下载.在这种情况下,您宁愿将锚标记与Url.Action
辅助程序结合使用:
<a href="@Url.Content("~/Content/Templates/Pt_Data/Pt_Data.xls")">
Download_Static_File
</a>
这假定您的Web应用程序在根目录下有一个名为Content/Templates/Pt_Data
的文件夹,其中包含一个名为Pt_Data.xls
的文件,当用户单击该锚标记时,该文件将由用户下载.
另一方面,如果用户要下载的文件位于无法从客户端公开访问的文件夹中(例如~/App_Data
文件夹),则在这种情况下,您可能需要执行控制器操作在将流文件的服务器上:
public ActionResult DownloadStaticFile(string filename)
{
string path = Server.MapPath("~/App_Data");
string file = Path.Combine(path, filename);
file = Path.GetFullPath(file);
if (!file.StartsWith(path))
{
throw new HttpException(403, "Forbidden");
}
return File(file, "application/pdf");
}
,然后在您的视图中,您将具有此控制器操作的锚点:
@Html.ActionLink(
linkText: "Download template",
actionName: "DownloadStaticFile",
controllerName: "Charge_Entry",
routeValues: new { filename = "Pt_Data.xls" },
htmlAttributes: null
)
I am working on an MVC 4 web application. On one page I am providing an anchor link which refers to a file on application's directory. The code of the same is -
@Html.Action("Download_Static_File", "Charge_Entry", new { File_Path = "../../Content/Templates/Pt_Data/Pt_Data.xls", File_Name = "Pt_Data_Template", value = "Download template" });
My motive is that the file should be downloaded on click.
However when I click the link, I get an error like
Could not find a part of the path 'C:\Program Files\Common Files\Microsoft Shared\Content\Templates\Pt_Data\Pt_Data.xls'.'
I also tried
System.Web.HttpContext.Current.Server.MapPath
which is giving this error:
OutputStream is not available when a custom TextWriter is used.
The action method being called is:
public FileResult Download_Static_File(string File_Path,string File_Name)
{
byte[] fileBytes = System.IO.File.ReadAllBytes(File_Path);
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, File_Name);
}
Is it the correct approach? Any help will be appreciated.
I also referred this link
Your anchor seem to be pointing to a controller action named Download_Static_File
(which unfortunately you haven't shown) and passing a parameter called File_Path
.
The @Html.Action
helper that you are using in your view is attempting to execute the specified action as a child action. You may find the following blog post useful which describes child actions: http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx/
I guess that what you are trying to achieve is to generate an anchor in your view pointing to the static file which can be downloaded by the user. In this case you'd rather use an anchor tag in conjunction with the Url.Action
helper:
<a href="@Url.Content("~/Content/Templates/Pt_Data/Pt_Data.xls")">
Download_Static_File
</a>
This assumes that your web application has a folder called Content/Templates/Pt_Data
under the root containing a file named Pt_Data.xls
which will be downloaded by the user when he clicks upon this anchor tag.
If on the other hand the file that you want to be downloaded by the user is situated in a folder which is not publicly accessible from the client (for example the ~/App_Data
folder) you might in this case have a controller action on your server that will stream the file:
public ActionResult DownloadStaticFile(string filename)
{
string path = Server.MapPath("~/App_Data");
string file = Path.Combine(path, filename);
file = Path.GetFullPath(file);
if (!file.StartsWith(path))
{
throw new HttpException(403, "Forbidden");
}
return File(file, "application/pdf");
}
and then in your view you would have an anchor to this controller action:
@Html.ActionLink(
linkText: "Download template",
actionName: "DownloadStaticFile",
controllerName: "Charge_Entry",
routeValues: new { filename = "Pt_Data.xls" },
htmlAttributes: null
)
这篇关于在Web应用程序中的服务器上提供静态文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!