来自@ url.action的文件路径

时间:2015-03-06 10:03:23

标签: asp.net asp.net-mvc razor

我的项目中有这个文件fontroller:

public class FilesController : Controller
{
    public FileResult Index(string path="", string filename="")
    {
        path = Path.GetFullPath(@"C:\\Users\\ikerib\\Pictures");
        filename = "header-vietnam.jpg";
        string dirPath = Path.Combine(path, filename);

        return File(dirPath, System.Net.Mime.MediaTypeNames.Application.Octet, filename);

    }
}

它从任何给定路径返回一个文件,例如“C:\ Files \ Filename.jpg”,它的目的是从外部Web根目录提供文件(图像),并且效果很好。

以前,我有一个列出所有可用文件的目录列表(Controller):

public ActionResult DocumentoLista(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Documento doc = db.Documentos.Find(id);
            if (doc == null)
            {
                return HttpNotFound();
            }
            string dirPath = Path.GetFullPath(doc.path);
            List<string> files = new List<string>();
            DirectoryInfo dirInfo = new DirectoryInfo(dirPath);
            foreach (string fInfo in Directory.EnumerateFiles(dirPath, "*.*", SearchOption.AllDirectories)
                            .Where(s => s.EndsWith(".png") 
                                || s.EndsWith(".jpg")  
                                || s.EndsWith(".pdf")
                             ).Select(Path.GetFileName)
                     )
            {
                files.Add(fInfo);
            }
            ViewBag.MyList = files;
            return View(doc);
        }

(视图)

<ul id="filelist" data-role="listview" data-inset="true" class="ui-listview ui-listview-inset ui-corner-all ui-shadow">
@foreach (var item in ViewBag.MyList)
    {        
        <li class="ui-li-has-thumb ui-first-child">
            <a href="@Url.Action("showdocument", "Frontend", new { id=1, FileName=@item})" class=" ui-btn ui-btn-icon-right ui-icon-carat-r">
                <img src=">@Url.Action("Index", "Files", new { path = Model.path, filename = item })">
                <h2>@item</h2>
                <p>@Url.Action("Index", "Files", new { path = Model.path, filename=item })</p>
                <p>@Html.ActionLink("Index", "Files", new { path = Model.path, filename = item })</p>
            </a>
        </li>
    }
</ul>

问题是在视图中文件路径是这样写的:

/Files?path=C%3A%5CUsers%5Cikerib%5CPictures&filename=header-vietnam.jpg

如何编写视图以正确地将路径发送到我的FilesController?

THX!

0 个答案:

没有答案