1

I want to save the path of a folder similar to what Google Chrome does:

enter image description here

I want the user to be able to select a folder and then this path will be saved to the database. I am just doing this on my local machine. I have found plenty of examples of how to save an image to a folder but I need to get the folder's path. Basically like the image, I need to create a button that will allow the user to select a folder from their machine. I need to save this folder path. The problem is how to I actually select the folder path and display it in the textbox?

user123456789
  • 1,914
  • 7
  • 44
  • 100
  • To be clear, you simply want to save text (the path) to a DB? –  Jan 04 '16 at 12:15
  • @JᴀʏMᴇᴇ yes like the image, I just want to save the path (as text) of the folder I select. Problem is that I don't know how to get the path of the folder – user123456789 Jan 04 '16 at 12:16
  • Show us what you have already regarding the controls on the page that are responsible for selecting the folder (both mark-up and code-behind). It's simply a case of retrieving the text value from that input and saving it in the DB. The *real* question is what do you already have in place for accessing data/writing data to your DB? –  Jan 04 '16 at 12:17
  • @JᴀʏMᴇᴇ that is the problem. I don't know what the code is for selecting a folder. – user123456789 Jan 04 '16 at 12:18
  • 1
    What do you have already? –  Jan 04 '16 at 12:18
  • @JᴀʏMᴇᴇ saving the text to the database isn't the problem. Actually selecting the folder is the issue here – user123456789 Jan 04 '16 at 12:21
  • Then the title of your question is totally off the mark. –  Jan 04 '16 at 12:21
  • @JᴀʏMᴇᴇ yes sorry I see it is causing confusion. What is the best way to describes this? – user123456789 Jan 04 '16 at 12:22
  • Talk us through a typical use case. And CLEARLY highlight which part of the story you are struggling to achieve. –  Jan 04 '16 at 12:23
  • @JᴀʏMᴇᴇ Basically like the image, I need to create a button that will allow the user to select a folder from their machine. I need to save this folder path. The problem is how to I actually select the folder path and display it in the textbox? – user123456789 Jan 04 '16 at 12:26
  • You can't do this, see http://stackoverflow.com/a/20537822/588079 for the full reason!! All you can do is ask the user to enter a path into a text-input. I don't really see what you want to do with that path however.. – GitaarLAB Jan 04 '16 at 17:00

2 Answers2

0

This will help you to get started I guess;

<div class="row">
    <label for="files" class="col-md-2">Select files : </label><input type="file" multiple class="control-label col-md-10" name="files">
</div>

You will be able to improve the layout by adding an input field, more labels etc. If you need even more, you can use jquery to improve further.

Kosala W
  • 2,133
  • 1
  • 15
  • 20
  • 2
    @Kosala I don't want to chose a file, I want to chose a folder – user123456789 Jan 04 '16 at 13:03
  • @The short answer is **NO**. It cannot be done unless you are willing to use an ActiveX. The above solution can be used for selecting a file/displaying a selected image in browser etc. [More Info....](http://www.faqs.org/rfcs/rfc1867.html) – Kosala W Jan 04 '16 at 13:42
  • Ok if I was to use an ActiveX how would I do this? – user123456789 Jan 04 '16 at 14:08
  • ActiveX is sort of an outdated technology. Only IE supports it fully. Here is a good article on [how to create an activeX and access it using a javascript](http://www.olavaukan.com/2010/08/creating-an-activex-control-in-net-using-c/). If you know vb6 (this is 90s technology remember..!!), it will be easier to create an ActiveX using vb6. As far as I can remember vb6 had a in-build folder browser. You just have to put that in to a user control to make your own activeX control. – Kosala W Jan 04 '16 at 22:04
-1

For security reasons you cannot access the folder in which a file that the user uploads to your application is stored in his client computer. So what you probably need is to store this uploaded file in some folder on your webserver and then store this folder in your database.

In order to get the physical location of some folder on your server you could use the MapPath method with a relative location:

string physicalLocation = Server.MapPath("~/App_Data/");

and then combine this with the filename:

string filename = Path.Combine(physicalLocation, "someFile.png");

Now you can store the filename location in your database.


UPDATE:

Unfortunately there's no such standard HTML control that will allow you to select a folder on the client machine. The closest you could get is a standard ASP.NET textbox where the user will simply type this folder:

<asp:TextBox runat="server" ID="SomeFolder" />

Now on the server side you could access the contents of this textbox and store in your database:

protected void SomeButtonClick(object sender, EventArgs e)
{
    string folder = SomeFolder.Text;
    // store the folder that the user typed in your database
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I think you're over-thinking it and have missed the point. OP appears to just want to save text to a DB. –  Jan 04 '16 at 12:14
  • No, he doesn't want to be saving any text in a db. What he wants is to save the folder on which some file is stored on the client machine which as I already explained in my answer is not possible for security reasons. Just read the comments he posted to your question and you will understand that. – Darin Dimitrov Jan 04 '16 at 12:16
  • Yes, I looked at his comment :-) Did you? Once you read it more carefully you will understand how wrong you were in your initial read of his question. – Darin Dimitrov Jan 04 '16 at 12:16
  • @DarinDimitrov sorry for any confusion about this question. I simply want to store the path of the folder in a database – user123456789 Jan 04 '16 at 12:17
  • @user123456789, is this path on the client machine where the user selects a file to upload? Or is it on the server where your application resides? – Darin Dimitrov Jan 04 '16 at 12:18
  • @DarinDimitrov it is on the client machine – user123456789 Jan 04 '16 at 12:21
  • OK, now read my answer once again. As I already explained to you **FOR SECURITY REASONS** you cannot get this information on the server. All you can get is the content of the uploaded file. This file can then be stored on a folder **ON YOUR SERVER** and then save this folder in your database. Is there still something that is not clear? – Darin Dimitrov Jan 04 '16 at 12:21
  • @DarinDimitrov - you're still not answering the question. Text from an input on a client can quite easily be saved in a DB on the server. What your posting has no relevance. This question is misleading, so you should be asking for clarification rather than attempting to answer. –  Jan 04 '16 at 12:23
  • @JᴀʏMᴇᴇ, I perfectly fine understood what the OP is asking. I am sorry if you didn't. I must agree that it wasn't a clear question in the first place. I wonder why you are so stubborn even after the OP made it explicitly clear that he wants to save the folder **ON THE CLIENT MACHINE** of an uploaded file to his database. – Darin Dimitrov Jan 04 '16 at 12:23
  • @DarinDimitrov - `I perfectly fine understood what the OP is asking` **or** `I must agree that it wasn't a clear question`. Choose one or the other. –  Jan 04 '16 at 12:24
  • @JᴀʏMᴇᴇ, why I must choose one or the other when the 2 statements are true :-) – Darin Dimitrov Jan 04 '16 at 12:25
  • @DarinDimitrov - `of an uploaded file` - who mentioned anything about uploading a file? What are you talking about? –  Jan 04 '16 at 12:25
  • OK, let's ask the OP then once again: @user123456789, does the user select a file to upload? – Darin Dimitrov Jan 04 '16 at 12:26
  • @DarinDimitrov no I am not trying to select a file. I want to select a folder path then save that to the database. The problem is I don't know the code to allow the user to select a folder – user123456789 Jan 04 '16 at 12:27
  • @DarinDimitrov - take a minute and breathe. Please, carefully, read the comment on his question (above). He's clearly specified what he wants. –  Jan 04 '16 at 12:27
  • Indeed. He is trying to make the user select some folder. I will delete my answer. – Darin Dimitrov Jan 04 '16 at 12:28
  • @DarinDimitrov so the only way to do this is to make the user type out the folder location into the textbox? – user123456789 Jan 04 '16 at 12:33
  • Yes, I am afraid that there's no standard HTML control which allows to explore the user's folder structure to select one. You could of course install some proprietary component on your clients machines like Flash or ActiveX but those are not standard and I prefer not to talk about such approaches as they will not work in the general case. – Darin Dimitrov Jan 04 '16 at 12:34
  • @DarinDimitrov ok but how does Google Chrome do this then? By clicking the change button I can browse for a folder – user123456789 Jan 04 '16 at 12:37
  • Google Chrome is a desktop application, not a web application. A desktop application can do much more on the client computer like opening a Browse Folders dialog if you will. A web application on the other hand is subject to restrictions. It runs in a sandbox (a browser) and can only perform a very limited set of operations on the client machine. Otherwise every website on the internet could have gained access to some precious information. – Darin Dimitrov Jan 04 '16 at 12:38
  • @DarinDimitrov `The closest you could get is a standard ASP.NET textbox` hang on - what about your own answer on a different question? Seems like a feasible option: http://stackoverflow.com/a/6716402/1017882 –  Jan 04 '16 at 12:41
  • @JᴀʏMᴇᴇ, I don't see how this answer relates to the problem here. In it I illustrate how a file can be downloaded. As we already established thanks to your help here, the OP is trying to select some folder on the client machine and send this information to the server. – Darin Dimitrov Jan 04 '16 at 12:43
  • @DarinDimitrov the title of the question, though, is `How to let user choose a folder?` –  Jan 04 '16 at 12:43
  • Yeah, yeah, the title is probably misleading. I thought we already agreed on that. – Darin Dimitrov Jan 04 '16 at 12:44
  • @DarinDimitrov - I meant the title of the question you answered previously (the one I just linked to). The titles are very similar. –  Jan 04 '16 at 12:49