【问题标题】:How can I create download links for all files in a directory in ASP.NET?如何为 ASP.NET 目录中的所有文件创建下载链接?
【发布时间】:2015-08-18 18:37:54
【问题描述】:

我有一个目录C:\Source\SomeDirectory\,其中包含任意数量的.pdf.txt 文件。我需要在我的 ASP.NET 网页上显示这些文件,并让它们中的每一个都是可下载的链接,例如:

其中每一个都是 .pdf 或 .txt。

如何为这些文件中的每一个创建链接?我知道他们都将在同一个目录中。我尝试过Returning a file to View/Download in ASP.NET MVC 之类的方法,但这只会返回一个文件?

相反,我需要返回一堆可下载的文件,并在我的页面上为它们提供链接以及其他普通 HTML 元素。我怎样才能做到这一点?

编辑:我已经编写了利用DirectoryInfo(path).GetFiles() 获取这些文件列表的代码。我将每个 fileName 及其 filePath 存储到我的视图模型中的对象列表中,并将其传递到我的视图中。我可以使用这些属性作为参数来进行下载吗?

显示如下:

<ul>
    <li><a href="@FileList[0].Path"><@FileList[0].FileName</a></li>
    <li><a href="@FileList[1].Path"><@FileList[1].FileName</a></li>
    <li><a href="@FileList[2].Path"><@FileList[2].FileName</a></li>
</ul>

【问题讨论】:

  • 您需要两个单独的操作结果。您需要一个列出所有文件的视图,并且您需要一个根据某些 URL 参数返回特定文件的操作结果。
  • 那么每个“链接”实际上会是一些 ActionResult() 调用,返回被点击的一个文件吗?
  • 正确,基于您在 URL 中传递的一些参数,例如 ID 号或文件名。
  • 那么您真正需要做的就是将物理路径转换为虚拟路径。这个问题可能会有所帮助:stackoverflow.com/questions/6081433/…
  • 为什么不在链接中指定文件名?只需获取文件的虚拟路径,然后将其设置为链接的 href。完毕。你让这件事变得比需要的困难得多。

标签: c# asp.net-mvc file-io download


【解决方案1】:

我最终做的是在 IIS 中将虚拟目录添加到我的 Web 应用程序的根目录。我将其命名为TestClassicWeb,并将其路径设置为包含文件的本地目录。

有关从页面获取文件的帮助,请参阅以下内容:

型号:

public class DisplayViewModel
{
    public List<DownloadableFile> FileList{ get; set; }
}

public class DownloadableFile
{
    public string FileName { get; set; }
    public string Path { get; set;}
}

控制器:

using System.Collections.Generic;   //for "List" type
using System.IO;    //GetFiles()

/* ... */

//identify the virtual path
string filePath = "/TestClassicWeb/Reports/SethRollins";

//map the virtual path to a "local" path since GetFiles() can't use URI paths
DirectoryInfo dir = new DirectoryInfo(Server.MapPath(quicklinksPath));

//Get all files (but not any subdirectories) in the folder specified above
FileInfo[] files = dir.GetFiles()

//iterate through each file, get its name and set its path, and add to my VM
foreach (FileInfo file in files )
{
    DownloadableFile newFile = new DownloadableFile();
    newFile.FileName = Path.GetFileNameWithoutExtension(file.FullName);     //remove the file extension for the name
    newFile.Path = filePath + file.Name;                        //set path to virtual directory + file name
    vm.FileList.Add(newFile);                                       //add each file to the right list in the Viewmodel
}

return (vm);

查看:

<ul>
    @foreach (DownloadableFile file in Model.FileList)
    {
        <li><a target="_blank" href="@file.Path">@file.FileName</a></li>
    }
</ul>

感谢 cmets 的帮助!

【讨论】:

  • 删除你真正应该使用的扩展:Path.GetFileNameWithoutExtension(file.Name)。它声明意图以及扩展长度不是 4(点 + 3)的比例。
【解决方案2】:

如 cmets 中所述,您可以将绝对路径转换为虚拟路径, 您也可以将它们的下载属性设置为可下载

<a href="/images/myw3schoolsimage.jpg" download>

根据W3School

下载属性指定当用户点击超链接时将下载目标。

此属性仅在设置了 href 属性时使用。

该属性的值将是下载文件的名称。对允许值没有限制,浏览器会自动检测正确的文件扩展名并将其添加到文件中(.img、.pdf、.txt、.html等)。

如果省略该值,则使用原始文件名。

虽然这受到限制,因为某些浏览器并不完全支持它。

【讨论】:

  • 是的...这在 IE 中根本不支持 哈哈。我想我会避免它几年....
猜你喜欢
  • 2012-12-18
  • 2019-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-19
相关资源
最近更新 更多