Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Display Files from a Folder in a View

What I'm looking to do, is display the contents of a folder which is located on my server in a View in my MVC application.

I have what I think should be in place for the Action, however I'm a little unsure how to go about implementing the corresponding view and I was wondering if someone could point be in the right direction on that. (and also, if someone thinks my Action could be improved, advice would be welcome :) )

Here is the action:

public ActionResult Index()
        {
            DirectoryInfo salesFTPDirectory = null;
            FileInfo[] files = null;

            try
            {
                string salesFTPPath = "E:/ftproot/sales";
                salesFTPDirectory = new DirectoryInfo(salesFTPPath);
                files = salesFTPDirectory.GetFiles();
            }
            catch (DirectoryNotFoundException exp)
            {
                throw new FTPSalesFileProcessingException("Could not open the ftp directory", exp);
            }
            catch (IOException exp)
            {
                throw new FTPSalesFileProcessingException("Failed to access directory", exp);
            }

            files = files.OrderBy(f => f.Name).ToArray();

            var salesFiles = files.Where(f => f.Extension == ".xls" || f.Extension == ".xml");

            return View(salesFiles);
        }

Any help would be appreciated, thanks :)

like image 513
109221793 Avatar asked Feb 17 '11 15:02

109221793


People also ask

What is a view in ASP NET MVC?

A view is used to display data using the model class object. The Views folder contains all the view files in the ASP.NET MVC application. A controller can have one or more action methods, and each action method can return a different view.

Where are MVC controller files stored in folder?

So, for easy maintenance, the MVC framework requires a separate sub-folder for each controller with the same name as a controller, under the Views folder. For example, all the views rendered from the HomeController will resides in the Views > Home folder.

What is the difference between views and controllers in MVC?

The Views folder contains all the view files in the ASP.NET MVC application. A controller can have one or more action methods, and each action method can return a different view. In short, a controller can render one or more views.

What is Razor View in ASP NET MVC?

View is a User Interface which displays data and handles user interaction. Views folder contains separate folder for each controller. ASP.NET MVC supports Razor view engine in addition to traditional .aspx engine. Razor view files has .cshtml or .vbhtml extension.


3 Answers

If you only want the file names then you can change your Linq query to

files = files.Where(f => f.Extension == ".xls" || f.Extension == ".xml")
  .OrderBy(f => f.Name)
  .Select(f => f.Name)
  .ToArray();
return View(files);

Then (assuming the default project template) add the following to the Index.cshtml view

<ul>
  @foreach (var name in Model) {
    <li>@name</li>
  }
</ul>

Which will display the list of file names

like image 163
David Glenn Avatar answered Oct 07 '22 13:10

David Glenn


  1. IMHO you should expose only what is really needed by the view. Think about it: do you really need to retrieve a whole FileInfo object, or only a file path? If the latter is true, just return a IEnumerable<string> to the view (instead of a IEnumerable<FileInfo>, which is what you're doing in the above code). Hint: just add a Select call to your Linq expression...
  2. Then your view will just render that model - what you need is a foreach loop and some HTML code to do it.
like image 30
rsenna Avatar answered Oct 07 '22 13:10

rsenna


This is a simplified example of a razor view. It will ouput your file names in a HTML table.

  @model IEnumerable<FileInfo>

    <h1>Files</h1>
    <table>

    @foreach (var item in Model) {
        <tr>
            <td>
                @item.Name
            </td>
        </tr>
    }

    </table>
like image 40
Banford Avatar answered Oct 07 '22 14:10

Banford