Click here to Skip to main content
Licence CPOL
First Posted 23 Sep 2007
Views 25,100
Downloads 870
Bookmarked 17 times

ASP FileOpen Dialog

By | 23 Sep 2007 | Article
This article will help in getting the fileopen dialog in an ASP.NET Web Application.

Introduction

"I want to use a "file open dialog box" control in my web application.", sounds like a common and most frequently asked question. Even I had the same question and started searching for a solution, but believe me, it was very hard to get a solution for this and with some help, I came out with a solution and thought I will share it with all my friends out here.

Using the Code

A web application of course wouldn't have any of the standard OS controls. It's a web application, not a fat client application. The only reason to ever present the user with a "File" dialog box is so they can upload a file. The standard HTML "file input" form element has been around for ages for just that reason: System.Web.UI.HtmlControls.HtmlInputFile.

That's the control I'll be using and working around to make things easier and achieve what I want. This is the code snippet which does all the magic:

<script type="text/javascript"language="javascript">

function getFile()
{
    document.getElementById("file1").click();
    var file = "";
    if(document.getElementById("TextBox1").value == "")
        file = document.getElementById("file1").value; 
    else
        file = document.getElementById("TextBox1").value + 
            "\r\n" + document.getElementById("file1").value;
    document.getElementById("TextBox1").value = file ;
}

</script>

Include this code in the header of the ASPX of the form.

The three controls which are needed are:

  1. ASP Button control
  2. <asp:ButtonID="Button1" runat="server" OnClick="Button1_Click" 
        Style="left: 637px; position: absolute; top: 303px" 
        Text="Button" Width="65px" /> 
  3. ASP TextBox control
  4. <asp:TextBoxID="TextBox1" runat="server" Height="12px" Width="9px">
    </asp:TextBox>
  5. HTML FileUpload control
  6. <inputid="File1" type="file" style=
     "width:119px;left:238px;position:absolute;top:41px;visibility:hidden;"
    />

Remember the "Visibility : Hidden" option in the style attribute of the HTML FileUpload control that is required and will do all the trick required to achieve what is required. It helps you hide the original HTML FileUpload control and lets all the work be done on button click, and then at the end, you can see the selected file path in the ASP TextBox control. You can place it in any of the other controls or use it for further processing.

Points of Interest

I hope this will help many others who are here searching for a solution, but remember, it works in Microsoft's IE only, but there's a solution to make it work in Firefox Mozilla too.. ha ha.. keep searching for that guys… good luck.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Govardhana Reddy

Web Developer
India
India India

Member

Follow on Twitter Follow on Twitter
This is Govardhana Reddy, i am here to explore this world of INTERNET. I feel this is one way through which i can explore this world of INTERNET.
 
I want to be one among the best in this profession (a Software Developer, not a Software Engineer its a bit Controversial.) if not the "BEST"
 
My definition of a Software Engineer : "A person who knows what to cut/copy and where to paste".
 
Apart from my technical stuff I love Long Drives, Computer Gaming, Sports, Bikes and much more to say.
 
Anyways long road ahead keep me accompanied...

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionReply Pinmemberyograjchaple21:43 7 May '12  
QuestionIt doesn't work PinmemberPeskind21:15 26 Dec '11  
QuestionWhy not use File Upload control? PinmemberManjit Dosanjh1:02 24 Sep '07  
Am I missing something? Why don't you use FileUpload control?
 
Then on the server side, you just type
 
FileUpload1.SaveAs(server.mappath("relative path"))
 
No other fiddly programming required
 
Neoptelemus
 
"If the facts don't fit the theory, change the facts."

AnswerRe: Why not use File Upload control? PinmemberGovardhana Reddy21:38 25 Sep '07  
AnswerRe: Why not use File Upload control? PinmemberPavel Yermalovich1:35 11 Oct '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 23 Sep 2007
Article Copyright 2007 by Govardhana Reddy
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid