用剃刀动态地从桌子上打开文件 [英] Open file from table dynamicaly with razor

查看:36
本文介绍了用剃刀动态地从桌子上打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用剃刀显示 Table ,其中包含具有一些不同详细信息的文件列表.我只想在单击他的名字时显示一个文件.

Hi i'm using razor to show a Table that contains list of file with some different details. I just want to show a file when i click on his name.

这是我的观点:

<table>
<tr>
    <th>
        Nom
    </th>
    <th>
        Date
    </th>
    <th>
        Uploader
    </th>
    <th></th>
</tr>

@foreach (var item in Model) {
<tr>
    <td>
          <a href = @Url.Action("ViewAttachment", new { fileName = item.Path }) > @Html.DisplayFor(modelItem => item.Nom) </a>  


    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Date)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Uploader)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.DocumentID }) |
        @Html.ActionLink("Details", "Details", new { id=item.DocumentID }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.DocumentID })
    </td>
</tr>
}

</table>

在我的操作中,我将文件的路径发送到控制器.但我不知道该如何处理.

In my action i send the path of my file to controller. but i don't know how to handle it.

public ActionResult ViewAttachment(string fileName)
    {
        try
        {
            return Redirect(filename);
        }
        catch
        {
            throw new HttpException(404, "Couldn't find " + fileName);
        }


    } 

当我单击它时,将我重定向到 domain/Document/Content/myfile ,但是我的文件在 domain/Content/myfile

When i click it redirect me to domain/Document/Content/myfile but my files are in domain/Content/myfile

推荐答案

当我单击他的名字时如何打开文件?

How to open the file when i click on his name ?

如果文件位于客户端可以直接访问的服务器上的某个位置,则不需要控制器操作,则可以直接将链接指向该位置:

If the file is on a location on the server that is directly accessible by the client you don't need a controller action, you could directly have the link point to this location:

<a href="@Url.Content("~/content/" + System.IO.Path.GetFileName(item.Path))"> 
    @Html.DisplayFor(modelItem => item.Nom) 
</a>  

如果无法从客户端访问该文件,则需要执行控制器操作以通过返回文件"结果来提供该文件:

If the file is not accessible from the client then you need a controller action to serve this file by returning a File result:

public ActionResult ViewAttachment(string fileName)
{
    fileName = System.IO.Path.GetFileName(fileName);
    string file = Server.MapPath("~/Content/" + fileName);
    if (!File.Exists(file))
    {
        return HttpNotFound();
    }  
    return File(file, fileName, "application/octet-stream");
}

如果要在新选项卡中打开目标文件,可以将 target ="_ blank" 属性添加到锚点:

And if you wanted to open the target file in a new tab you could add the target="_blank" attribute to the anchor:

<a href="@Url.Content("~/content/" + System.IO.Path.GetFileName(item.Path))" target="_blank"> 
    @Html.DisplayFor(modelItem => item.Nom) 
</a>  

这篇关于用剃刀动态地从桌子上打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆