I am trying to create a FileOpen Dialog for my website in C#, whereby i want the starting directory to be my images folder and only want to select .jpg, .png & .gif files. I also want the dialog box to return the address as though the root was my images folder i.e.

Images\Gallery\Wedding Photos\image1.jpg

can anyone help me?

Dani AI

Generated

Two different problems are mixed together here, and the solution depends on which one you mean.

If you want the user to pick a local file from their PC (an upload): browsers do not let a web page set the File Open dialog starting folder or reveal the client’s full path for privacy reasons. Use a file input with an accept filter to limit shown file types, but always re-check on the server after upload:

<input type="file" accept=".jpg,.jpeg,.png,.gif" />

See the specs and security notes for the file input element on MDN: HTML input type=file. Note that browsers intentionally hide the real client path (you may see C:\fakepath\...), so you cannot get a client-relative path like Images\Gallery\....

If you meant letting the user choose an image already on your web server (so you can return a server-relative address like Images/Gallery/Wedding Photos/image1.jpg): build a server-side file browser. Steps a simple implementation should follow:

  • Enumerate files under your Images folder and filter by extension.
  • Render a tree or list in HTML/JS (a modal or popup works well).
  • When a file is picked, return a server-relative path (no server absolute paths) and use that path for your image links.

A minimal server-side approach in C# uses Directory.EnumerateFiles to find files and then strip the physical root to produce relative paths; see the .NET docs for enumerating files: Directory.EnumerateFiles. As noted, a JavaScript file-browser UI or an ASP.NET page that lists server files is the right direction if the files already live on the server.

Always validate file types and permissions on the server side, and avoid exposing absolute filesystem paths to clients.

Recommended Answers

All 2 Replies

you cant do that with drag and drop toolbox controls. you need javascript file browser or asp.net file browser. search google with these.

you cant do that with drag and drop toolbox controls. you need javascript file browser or asp.net file browser. search google with these.

Thanks Serkan,

will try this!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.