I am devoloping a Web Application, in which one Page has some neccessity that User should select any folder in the System and for ex if he selects ex:D:xyz, then this Path should be Saved in Database, I have written the Code for Storing in to Database and Remaining Every thing. But actually i think that if it is like a Browse Button click function to select the Folder, what i have made is, as i dont know how to make it in Web Application simply i used a textbox to be able to user to type the Path. Can, any one help me how to apply the Open File Dialog Functionality in a Web Page?
Thanks in Advance,
Regards
Kalyan.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
There is no ASP.Net Control to select folders.
As a simple workaround you could use the FileUpload-Control to let the user select a file and you can save the folder of that file. But that would work only in IE because other browsers don’t post the full file-path but only the file-name(security restrictions).
Edit: As Ben mentioned that doesn’t work in IE>7 either:
http://blogs.msdn.com/b/ie/archive/2008/07/02/ie8-security-part-v-comprehensive-protection.aspx (section File Upload Control)
Method 2
For Web Application:
System.IO.FileInfo file = new System.IO.FileInfo(file1);
Response.AddHeader("Content-Disposition", "attachment;filename="" +"Filename"+".ext"+ """);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = file.Extension.ToLower();
Response.WriteFile(file.FullName);
Response.End();
Method 3
Add windows openfile dialog reference toweb application by righ click on solution explore project name add reference system.windows.forms
then follow this coding style here i have given VB sample code you can convert this to C# if your facing any problem tell me.
VB Code
Import system.threading
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
Dim objThread As New Thread(AddressOf FnOpenFileDialog)
objThread.IsBackground = True
objThread.SetApartmentState(ApartmentState.STA)
objThread.Start()
End Sub
Private Sub FnOpenFileDialog()
Dim openfile As New System.Windows.Forms.OpenFileDialog
'openfile.InitializeLifetimeService()
'openfile.Filter = String.Format("Image file (*.jpg)|*.jpg|All files (*.*)|*.*")
openfile.Filter = String.Format("Image file (*.jpg)|*.jpg")
openfile.Multiselect = True
openfile.ShowDialog()
End Sub
Method 4
- Is the user selecting a file or a folder?
- Is the location on the server, or on his local computer?
- Are you trying to upload something, or download something, or just want the path?
If you want the path of a server file or folder, you have to get the server to give a list of possible folders to choose from.
If you just want the path of a local file, you cannot do this from a web application.
The easiest solution is to tell the user to browse in Explorer and copy the path, to paste in your dialog.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0