C# 如何为ASP.NET目录中的所有文件创建下载链接?
我有一个目录C# 如何为ASP.NET目录中的所有文件创建下载链接?,c#,asp.net-mvc,file-io,download,C#,Asp.net Mvc,File Io,Download,我有一个目录C:\Source\SomeDirectory\,它有任意数量的.pdf和.txt文件。我需要在我的ASP.NET网页上显示这些文件,并让每个文件都可以下载链接,例如: 其中每一个都是.pdf或.txt 如何为这些文件中的每一个创建链接?我知道它们都在同一个目录中。我尝试过类似的方法,但这只返回一个文件 相反,我需要返回一堆可下载的文件,并在我的页面上与其他普通HTML元素建立链接。我怎样才能做到这一点 Edit:我已经编写了一些代码,它们利用DirectoryInfo(path)
C:\Source\SomeDirectory\
,它有任意数量的.pdf
和.txt
文件。我需要在我的ASP.NET网页上显示这些文件,并让每个文件都可以下载链接,例如:
其中每一个都是.pdf或.txt
如何为这些文件中的每一个创建链接?我知道它们都在同一个目录中。我尝试过类似的方法,但这只返回一个文件
相反,我需要返回一堆可下载的文件,并在我的页面上与其他普通HTML元素建立链接。我怎样才能做到这一点
Edit:我已经编写了一些代码,它们利用DirectoryInfo(path).GetFiles()
来获取这些文件的列表。我将每个文件名
及其文件路径
存储到我的viewmodel中的对象列表中,并将其传递到我的视图中。我可以使用这些属性作为参数来进行下载吗
它的显示方式如下:
<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>
- 如注释中所述,您可以将绝对路径转换为虚拟路径,
您还可以将它们的下载属性设置为可下载
<a href="/images/myw3schoolsimage.jpg" download>
“下载”属性指定当用户单击超链接时将下载目标
此属性仅在设置了href属性时使用
该属性的值将是下载文件的名称。允许的值没有限制,浏览器将自动检测正确的文件扩展名并将其添加到文件(.img、.pdf、.txt、.html等)
如果省略该值,则使用原始文件名
尽管这是有限的,因为一些浏览器不完全支持它。如注释中所述,您可以将绝对路径转换为虚拟路径,
您还可以将它们的下载属性设置为可下载
<a href="/images/myw3schoolsimage.jpg" download>
“下载”属性指定当用户单击超链接时将下载目标
此属性仅在设置了href属性时使用
该属性的值将是下载文件的名称。允许的值没有限制,浏览器将自动检测正确的文件扩展名并将其添加到文件(.img、.pdf、.txt、.html等)
如果省略该值,则使用原始文件名
尽管这是有限的,因为一些浏览器不完全支持它。如注释中所述,您可以将绝对路径转换为虚拟路径,
您还可以将它们的下载属性设置为可下载
<a href="/images/myw3schoolsimage.jpg" download>
“下载”属性指定当用户单击超链接时将下载目标
此属性仅在设置了href属性时使用
该属性的值将是下载文件的名称。允许的值没有限制,浏览器将自动检测正确的文件扩展名并将其添加到文件(.img、.pdf、.txt、.html等)
如果省略该值,则使用原始文件名
尽管这是有限的,因为一些浏览器不完全支持它。如注释中所述,您可以将绝对路径转换为虚拟路径,
您还可以将它们的下载属性设置为可下载
<a href="/images/myw3schoolsimage.jpg" download>
“下载”属性指定当用户单击超链接时将下载目标
此属性仅在设置了href属性时使用
该属性的值将是下载文件的名称。允许的值没有限制,浏览器将自动检测正确的文件扩展名并将其添加到文件(.img、.pdf、.txt、.html等)
如果省略该值,则使用原始文件名
虽然这是有限的,因为一些浏览器不完全支持它。我最后做的是在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>
查看:
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>
@foreach(Model.FileList中的可下载文件)
{
}
感谢评论中的帮助 我最后做的是在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>
查看:
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>
@foreach(Model.FileList中的可下载文件)
{
}
感谢评论中的帮助 我最后做的是在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>
查看:
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>
@foreach(Model.FileList中的可下载文件)
{
}
感谢评论中的帮助 我最后做的是在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>
查看:
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>
@foreach(Model.FileList中的可下载文件)
{
}
感谢评论中的帮助 你需要