C# 基于mvc asp条件从文件夹中获取文件

C# 基于mvc asp条件从文件夹中获取文件,c#,asp.net-mvc,file,download,C#,Asp.net Mvc,File,Download,我有两个表,Note和file(一对多)。 我将所有文件保存在“files”文件夹中,然后除了NoteID(FK)之外,我还将名称、路径和扩展名保存在文件表中 我成功上传了它们。 但是当我想下载基于NoteID条件的文件时,我不能。 我可以得到该文件夹中的所有文件 我的问题是如何根据条件从特定文件夹中获取一些文件? 我试过几种解决办法,但都不管用 有什么帮助吗 行动: public ActionResult Download(int NoteID) { var fs = db.File

我有两个表,Note和file(一对多)。 我将所有文件保存在“files”文件夹中,然后除了NoteID(FK)之外,我还将名称、路径和扩展名保存在文件表中

我成功上传了它们。 但是当我想下载基于NoteID条件的文件时,我不能。 我可以得到该文件夹中的所有文件

我的问题是如何根据条件从特定文件夹中获取一些文件? 我试过几种解决办法,但都不管用

有什么帮助吗

行动:

public ActionResult Download(int NoteID)
{

    var fs = db.Files.Where(f => f.NoteID == NoteID);

    string[] files = Directory.GetFiles(Server.MapPath("/Files"));


    for (int i = 0; i < files.Length; i++)
    {
        files[i] = Path.GetFileName(files[i]);
    }
    ViewBag.Files = files;
    return View();
}

public FileResult DownloadFile(string fileName)
{
    var filepath = System.IO.Path.Combine(Server.MapPath("/Files/"), fileName);
    return File(filepath, MimeMapping.GetMimeMapping(filepath), fileName);
}
public ActionResult下载(int-NoteID)
{
var fs=db.Files.Where(f=>f.NoteID==NoteID);
string[]files=Directory.GetFiles(Server.MapPath(“/files”);
for(int i=0;i
下载视图:

@{
    ViewBag.Title = "Download Files";
    var Files = (ViewBag.Files as string[]);

    if (Files != null && Files.Any())
    {
        foreach (var file in Files)
        {
            <br />
            @Html.ActionLink(file, "DownloadFile", new { fileName = file })
            <br />

        }
    }
    else
    {
        <label>No File(s) to Download</label>
    }
}
@{
ViewBag.Title=“下载文件”;
var Files=(ViewBag.Files作为字符串[]);
if(Files!=null&&Files.Any())
{
foreach(文件中的var文件)
{

@ActionLink(文件“DownloadFile”,新的{fileName=file})
} } 其他的 { 没有要下载的文件 } }
编辑:

上面的代码正在运行,获取并显示“文件”文件夹中的所有文件

当我试着运行下面的代码时,我得到了IOEception

行动:

  public ActionResult Download(int? NoteID)
    {


        var fs = db.Files.Where(f => f.NoteID == NoteID);


        string[] files = new string[(fs.Count()) - 1];



        int counter = 0;

        foreach (var item in fs)
        {
            files[counter] = Directory.GetFiles(Server.MapPath("/Files/"+item.FileName)).Single();
            /*here is the Exception
             * 
            System.IO.IOException:
            { "The directory name is invalid.\r\n"}

            InnerException message:
            The directory name is invalid.    
            */

        }



        for (int i = 0; i < files.Length; i++)
        {
            files[i] = Path.GetFileName(files[i]);
        }
        ViewBag.Files = files;
        return View();
    }

    public FileResult DownloadFile(string fileName)
    {
        var filepath = System.IO.Path.Combine(Server.MapPath("/Files/"), fileName);
        return File(filepath, MimeMapping.GetMimeMapping(filepath), fileName);
    }
public ActionResult下载(int?NoteID)
{
var fs=db.Files.Where(f=>f.NoteID==NoteID);
string[]files=新字符串[(fs.Count())-1];
int计数器=0;
foreach(fs中的var项目)
{
files[counter]=Directory.GetFiles(Server.MapPath(“/files/”+item.FileName)).Single();
/*这是个例外
* 
System.IO.IOException:
{“目录名无效。\r\n”}
InnerException消息:
目录名无效。
*/
}
for(int i=0;i
视图:

@{
ViewBag.Title=“下载文件”;
var Files=(ViewBag.Files作为字符串[]);
if(Files!=null&&Files.Any())
{
foreach(文件中的var文件)
{

@ActionLink(文件“DownloadFile”,新的{fileName=file})
} } 其他的 { 没有要下载的文件 } }
在新代码中:

files[counter] = Directory.GetFiles(Server.MapPath("/Files/"+item.FileName))
您使用的参数不正确。您正在传递文件路径,但此方法需要目录路径

您可以使用以下选项:

var filteredByFilename = Directory
                        .GetFiles(Server.MapPath("/Files"))
                        .Select(f => Path.GetFileName(f))
                        .Where(f => f.StartsWith("yourFilename"));

ViewBag.Files = filteredByFilename.ToArray();
但在您的情况下,我认为最好使用Directory.EnumerateFiles方法。请阅读

因此,新的准则是:

var filteredByFilename = Directory
                        .EnumerateFiles(Server.MapPath("/Files"))
                        .Select(f => Path.GetFileName(f))
                        .Where(f => f.StartsWith("yourFilename"));

ViewBag.Files = filteredByFilename.ToArray();
整个下载方法可以是:

public ActionResult Download(int NoteID)
{
    var fs = db.Files.Where(f => f.NoteID == NoteID);

    var fileNames = Directory
        .EnumerateFiles(Server.MapPath("/Files"))
        .Select(f => Path.GetFileName(f));

    var result = new List<string>();

    foreach (var fileName in fs)
    {
        var filteredByName = fileNames.Where(f => f.StartsWith(fileName));
        result.AddRange(filteredByName);
    }

    ViewBag.Files = result.ToArray();
    return View(); 
}
public ActionResult下载(int-NoteID)
{
var fs=db.Files.Where(f=>f.NoteID==NoteID);
var fileNames=目录
.EnumerateFiles(Server.MapPath(“/Files”))
.Select(f=>Path.GetFileName(f));
var result=新列表();
foreach(fs中的var文件名)
{
var filteredByName=文件名。其中(f=>f.StartsWith(fileName));
result.AddRange(filteredByName);
}
ViewBag.Files=result.ToArray();
返回视图();
}
在新代码中:

files[counter] = Directory.GetFiles(Server.MapPath("/Files/"+item.FileName))
您使用的参数不正确。您正在传递文件路径,但此方法需要目录路径

您可以使用以下选项:

var filteredByFilename = Directory
                        .GetFiles(Server.MapPath("/Files"))
                        .Select(f => Path.GetFileName(f))
                        .Where(f => f.StartsWith("yourFilename"));

ViewBag.Files = filteredByFilename.ToArray();
但在您的情况下,我认为最好使用Directory.EnumerateFiles方法。请阅读

因此,新的准则是:

var filteredByFilename = Directory
                        .EnumerateFiles(Server.MapPath("/Files"))
                        .Select(f => Path.GetFileName(f))
                        .Where(f => f.StartsWith("yourFilename"));

ViewBag.Files = filteredByFilename.ToArray();
整个下载方法可以是:

public ActionResult Download(int NoteID)
{
    var fs = db.Files.Where(f => f.NoteID == NoteID);

    var fileNames = Directory
        .EnumerateFiles(Server.MapPath("/Files"))
        .Select(f => Path.GetFileName(f));

    var result = new List<string>();

    foreach (var fileName in fs)
    {
        var filteredByName = fileNames.Where(f => f.StartsWith(fileName));
        result.AddRange(filteredByName);
    }

    ViewBag.Files = result.ToArray();
    return View(); 
}
public ActionResult下载(int-NoteID)
{
var fs=db.Files.Where(f=>f.NoteID==NoteID);
var fileNames=目录
.EnumerateFiles(Server.MapPath(“/Files”))
.Select(f=>Path.GetFileName(f));
var result=新列表();
foreach(fs中的var文件名)
{
var filteredByName=文件名。其中(f=>f.StartsWith(fileName));
result.AddRange(filteredByName);
}
ViewBag.Files=result.ToArray();
返回视图();
}

您能解释一下接收异常的位置吗?System.IO.IOException:{“目录名无效。\r\n”}InnerException消息:目录名无效。在操作DownloadFile()中,您是否验证了哪个值具有
文件路径
?因为您有那个异常,这意味着您在定位文件时遇到了问题。对吗?@kat1330实际上,编译器在调用DownloadFile操作之前就停止了,您知道接受文件路径作为参数并检索该文件的方法吗。因为我想将其存储在字符串数组中。您能解释一下接收异常的位置吗?System.IO.IOException:{“目录名无效。\r\n”}InnerException消息:目录名无效。在操作DownloadFile()中,您是否验证了哪个值具有
文件路径
?因为您有那个异常,这意味着您在定位文件时遇到了问题。对吗?@kat1330实际上,编译器在调用DownloadFile操作之前就停止了,您知道接受文件路径作为参数并检索该文件的方法吗。因为我想将它存储在字符串数组中。谢谢,但是对于我的情况,什么是合适的方法呢?我的意思是,当我传递文件路径时,检索文件的方法是什么?它可以工作,但不幸的是它只检索一个文件。如何添加其余文件,因为我