Home > front end >  How to get files seperately to their extensions in MVC?
How to get files seperately to their extensions in MVC?

Time:01-28

I wrote a function that when user clicks on button for pdf, user will only show the pdf documents. I'm having a difficulty about where to put returns in this function.

   public IActionResult GetFiles(string dir)  {
        if ((dir == null) || (!Directory.Exists(dir))) { return BadRequest(); }
        var filesList = new List<FileImage>();
        var pdffilesList = new List<FileImage>();
        var dirInfo = new DirectoryInfo(dir);
        var files = dirInfo.GetFiles();
        foreach (var file in files)
        {
            if (file.Extension.Contains(".pdf"))
            {
                pdffilesList.Add(new FileImage
                {
                    Name = file.Name,
                    FullName = Regex.Match(file.FullName, "wwwroot(.*)").Groups[1].Value,
                    LastWriteTime = file.LastWriteTime.ToString("yyyy.MM.dd HH:mm"),
                    DirectoryName = file.DirectoryName,
                    Length = file.Length,
                    Extension = file.Extension
                });
                return Ok(pdffilesList);
            }
            else 
            {
            filesList.Add(new FileImage
            {

                Name = file.Name,
                FullName = Regex.Match(file.FullName, "wwwroot(.*)").Groups[1].Value,
                LastWriteTime = file.LastWriteTime.ToString("yyyy.MM.dd HH:mm"),
                DirectoryName = file.DirectoryName,
                Length = file.Length,
                Extension = file.Extension
            });            
            }
           
        }
        return Ok(pdffilesList);
    }    

What should I change in here ?

CodePudding user response:

If you just want the PDF files, then don't do anything to include the others:

    foreach (var file in files)
    {
        if (file.Extension.Contains(".pdf"))
        {
            pdffilesList.Add(new FileImage
            {
                Name = file.Name,
                FullName = Regex.Match(file.FullName, "wwwroot(.*)").Groups[1].Value,
                LastWriteTime = file.LastWriteTime.ToString("yyyy.MM.dd HH:mm"),
                DirectoryName = file.DirectoryName,
                Length = file.Length,
                Extension = file.Extension
            });
            // note: removed this `return`
            //return Ok(pdffilesList);
        }
        // It's not a .pdf, so ignore it
    }
    return Ok(pdffilesList);

You might also consider whether you really want Contains here. Do you want to include files with extensions like ".pdfqpz" or "foopdf"? Contains will give you any file whose extension contains the string "pdf" anywhere in it. You also will miss files that have extensions ".PDF" or ".pDf", etc. You probably want something like:

if (file.Extension.Equals(".pdf", StringComparison.InvariantCultureIgnoreCase))

CodePudding user response:

DirectoryInfo.GetFiles and EnumeratFiles can be used with a pattern. Instead of using separate branches you can use separate patterns:

var pattern=onlyPdfs? "*.pdf":"*";
var regex=new Regex("wwwroot(.*)");
var files=dirInfo.EnumerateFiles(pattern)
                 .Select(fi=>new FileImage 
                 {
                    Name = file.Name,
                    FullName = regex.Match(file.FullName).Groups[1].Value,
                    LastWriteTime = file.LastWriteTime.ToString("yyyy.MM.dd HH:mm"),
                    DirectoryName = file.DirectoryName,
                    Length = file.Length,
                    Extension = file.Extension
                 });
 return Ok(pdffilesList);

GetFiles waits until it finds all files and returns them in an array. EnumerateFiles on the other hand produces an IEnumerable<FileInfo> which returns each file as soon as it's found

  • Related