Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I create an Open File Dialog? 1

Status
Not open for further replies.

AHint

Programmer
Sep 12, 2003
20
GB
I have a web page that I want users to be able to open files from. The files will all be text files that contain SQL that I would then run against a database.

How do I get a new Open File Dialog window. In a windows application I would use System.Windows.Forms.OpenFileDialog. What is the equivalent ASP.net solution.

Thanks in advance.
 
Drop an HTML file input control onto the page. When the user clicks "Browse...", it will spawn the file dialog.

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
Thanks, another question:

How can I limit the file types that are displayed and limit the directory paths available?

Also, how should I then work with the selected file? It's not "runat="server"" so I can't use the C# code behind.

I'm new to this so sorry if these all seem like dumb questions!
 
Add the runat=server to the html tag. This will allow you to access all of the control's methods etc. in the postback code.

When you add the runat=server to the html input file control, it can be accessed from codebehind as an HtmlInputFile control:
HtmlInputFile members:
You can use the HtmlInputFile.Accept property to filter file types:
-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
Thanks again.

All seems to work now one way or another. What I'm actually trying to do is read a .txt or .sql file from the users PC into my application which will then display the sql in a textbox and the user then runs the sql from there.

I've got it working doing the following steps but would be interested to hear any other suggestions:

- accept property set to @"text/plain"
- this stops most files being loaded but didn't stop excel files being picked up. All files were still displayed in the file viewer.
- is there a way to restrict the files being displayed?

- check that files has a .txt or .sql ending and proceed if successful

- use the HttpPostedFile class to save the text file to web server and then read this file using a streamreader into the textbox
- is there anyway of bypassing copying the file to my web server so that I can read the file directly across a network.

At the end of the day it's working now but in the interest of efficiency and my own learning curve (still very steep!!) I would be interested to hear any ideas.
 
- accept property set to @"text/plain"
I'm not too sure about this - haven't actually used this property before, but maybe change this to ("text/*,sql/*")?

As for displaying the contents without uploading the file, you should be able to access the HtmlInputFile control's InputStream to do this. Maybe something like this?:

Code:
System.IO.Stream _stream = fileUpload.PostedFile.InputStream;
byte[] _buffer = new byte[_stream.Length];
_stream.Read(_buffer, 0, _stream.Length);

myTextBox.Text = System.Text.ASCII.ASCIIEncoding.GetString(_buffer);

If there are any errors in the above code (there very well may be... just beginning my day 'n working on my first coffee...) it's not my fault ;) It's just there to give you the idea. The above should work for plaintext files. Anything else (i.e. word docs, excel files) and you'll have to parse through all the extra added stuffs.

Hope that helps.

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
Well if that's after one coffee I think I should be worried that I am on my tenth (GMT), fully awake and still only just on the safe side of clueless!!

Reading the stream of data worked a treat - thanks very much again for your help!!!

Anyone reading this in the future I had to make one very minor change. In AtomicChip's code I had to change the following syntax:

System.Text.ASCII.ASCIIEncoding.GetString(_buffer)

became:

System.Text.ASCIIEncoding.ASCII.GetString(_buffer)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top