1

In MVC c#. asp.net.

The requirement that I have is for the user to be able to select a directory and from there, as long as the files within the directory start with "Anqu" and are .pdf files, I need to upload all those files to a server.

Since there is not easy way to do a folder chooser, I thought what I would do is to have the user select a file from a folder and from there, I could retrieve the directory path and from thre I can retrieve all the files that fit the requirements.

This is the best approach that I found(please comment if you feel differently).

Once the user selects a file and hits submit, it goes to the following action result:

     [HttpPost]
     public ActionResult FileUpload(HttpPostedFileBase file) 

     string dirName = new DirectoryInfo(file.FileName).Name; 

to get the directory info. I am finding that it is not possible to get it.

This is what my view looks like:

    @using (Html.BeginForm("FileUpload", "Plt", FormMethod.Post, new { enctype = "multipart/form-data" }))

    {          

       <input type="file" name="file" />
       <input type="submit" value="OK" />
    }
Nate Pet
  • 38,422
  • 114
  • 251
  • 393

3 Answers3

1

You can't do that in js. Javascript in a browser can't access the local filesystem. The file you manipulate is a stream from the request. You can't access the client filesystem from the server. That's not how http works.

You should check uploaders that provide nice ui and allows for multiple file uploads.

Hylaean
  • 1,139
  • 11
  • 16
1

You can't manipulate or access the client side file system, as you can imagine this would be very dangerous if it's allowed.

Imagine going to a website, and then all your private files are uploaded to the server without you specifically choosing to upload them to that server!!

That's why you can only upload file that the user chose to upload to your server, the best you can do is to allow multiple files upload async, using javascript/jquery or using telerix mvc controls

Bassam Mehanni
  • 14,186
  • 2
  • 30
  • 41
  • The Telerik file upload has saved me lots of programming hours. I highly recommended it. It handles multiple uploads async or regular postback. – JoshYates1980 Sep 03 '14 at 21:13
0

It sounds like you're trying to get a user to upload files from their computer to the server (as is generally the case). The first issue you'll run into is that

string dirName = new DirectoryInfo(file.FileName).Name;

is executing on the server. You could use a multiple file uploader like uploadify, and set the wildcard selection to Anqu*.pdf, so that only files which match your description are displayed.

Uploadify with ASP.Net MVC question

Community
  • 1
  • 1
Joe Carr
  • 313
  • 1
  • 9