Evan
Evan

Reputation: 57

Can a ActionResult open a specific folder on a server?

For example, I need only documents from a folder on the server drive X:\Docs for an online web application. Is there a way that a button on the website will open X:\Docs by default? I have tried this to open specific folders with no luck:

[HttpPost]
public ActionResult Index(HttpFileCollection file)
{
    var path = System.IO.Path.GetDirectoryName("X:\Docs");
    return RedirectToAction("Index");
}

I am new to C# and MVC. Is this achievable?

Upvotes: 1

Views: 249

Answers (1)

Steve
Steve

Reputation: 11973

You can enable directory browsing of that folder and then having the button (or href) to point to the url. You don't event need a controller method for it.

Updated: if the folder is not under your website's root you will need to do some work by yourself. For example

@foreach (string path in Directory.GetFiles("X:\Docs"))
{
    <div>
       <!--doc link-->
    </div>
}

You will need to have read permission for that drive ofc

As Luke pointed out you could alo do this inside your controller and pass it into your View which I also think it might be a better approach since View should be responsible for reading and rendering data

Upvotes: 4

Related Questions