【问题标题】:What needs to happen for a link inside a Kendo grid to open a file?剑道网格内的链接打开文件需要做什么?
【发布时间】:2015-08-04 16:38:14
【问题描述】:
我在 MVC 项目中有一个 Kendo 网格,其中一列是文件的链接。当用户单击链接时,我希望文件在新选项卡中打开或至少下载。相反,当单击链接时,什么也没有发生。文件路径正确;如果我将其复制并粘贴到浏览器窗口中,它将打开该文件。我的想法是剑道网格中的某些东西阻止了链接。谁能告诉我如何获取打开文件的链接?
这是网格中的相关列:
columns.Bound(m => m.CoDocsName).Title("Name")
.ClientTemplate(
"<a href='#= CoDocsPath#'>#= CoDocsName #</a>"
);
链接解析为“file:///C:/inetpub/wwwroot/Attachments/files.txt”。如果您将鼠标悬停在网格中的链接上,您可以看到此路径。如果您将其复制并粘贴到浏览器窗口中,它将显示该文件。单击网格内的链接不会执行任何操作。
【问题讨论】:
标签:
asp.net-mvc
razor
kendo-ui
kendo-grid
kendo-asp.net-mvc
【解决方案1】:
由于您正在使用 MVC,我建议您调用一种操作方法从服务器下载文件,而不是使用 HREF。这将需要以下步骤:
- 使用文件名从控制器调用下载操作方法。
- 确定从中获取文件的完整路径并确定其 MIME 类型以正确的格式下载文件。
- 最后使用附加到响应头的 ContentDisposition 来启动下载。
这是供您使用的易于使用的客户端和服务器端代码:
调用服务器调用的客户端模板:
columns.Template(@<text></text>).Width("6%")
.Title("Download")
.ClientTemplate("<a href='" +
Url.Action("Download", "Attachment", new { fileName= "#=FileName#" }) +
"'><i class='icon-download'></i></a> ");
服务器端操作方法:
public ActionResult Download(string fileName)
{
if (fileName != null)
{
string path = @""; //eg "C:\\Attachment"
string fullPath = Path.Combine(path, fileName);
var contentType = MimeMapping.GetMimeMapping(fullPath);
var fileBytes = SysFile.ReadAllBytes(fullPath);
// Convert to ContentDisposition
var cd = new System.Net.Mime.ContentDisposition
{
FileName = fileName,
Inline = false,
};
Response.AppendHeader("Content-Disposition", cd.ToString());
// View document
return File(fileBytes, contentType);
}
return null;
}
请告诉我它是如何为你工作的。