Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 来自@url.action的文件路径_Asp.net_Asp.net Mvc_Razor - Fatal编程技术网

Asp.net 来自@url.action的文件路径

Asp.net 来自@url.action的文件路径,asp.net,asp.net-mvc,razor,Asp.net,Asp.net Mvc,Razor,我的项目中有以下文件: public class FilesController : Controller { public FileResult Index(string path="", string filename="") { path = Path.GetFullPath(@"C:\\Users\\ikerib\\Pictures"); filename = "header-vietnam.jpg"; string dir

我的项目中有以下文件:

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根目录外部提供文件和图像,它工作得非常好

以前,我有一个目录列表,列出了所有可用文件,如以下控制器:

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);
        }
如何编写视图以将路径正确发送到FileController


谢谢

允许web应用程序访问其根文件夹之外的任何文件是一个非常糟糕的想法。而且它在生产环境中不能像在Visual Studio中那样开箱即用……他们需要指定一个服务器文件路径网络驱动器,用于存储图像文件,我需要在客户端浏览器上为它们提供服务,因为它们不能具有相同的映射驱动器。它仅供我的企业使用。你知道另一种方法吗?我会将图像存储在根web文件夹下的文件夹中,从而避免访问它之外的路径。或者您只需要在所需文件夹上设置FTP服务,而不是为此创建应用程序。图像现在存储在服务器文件夹中,并且需要在那里。他们不想上传任何东西,他们只想将这些文件提供给浏览器。这是强制性的。这些映像由用户创建并存储在映射的网络驱动器上。
<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