On a fine working day, I got a task from my project lead. The task was very simple - I had to create a file uploader in ASP.NET. When I started doing that task, he gave me some requirements.The Requirements are:
- The file uploader should contain only one button with text as "Choose File.."
- I should not use any Ajax file Uploaders.
- I should not use any swf Uploaders.
I jusk said ok, no problem and I searched everywhere in Google forums and even in CodeProject, but I didn't get any solution for that problem. Suddenly I got an idea of doing that task using CSS and finally I succeeded. Using the Code
<form id="form1" runat="server">
<div style="height: 89px; width: 620px; position: relative; top: 226px; left: 229px; overflow:hidden">
<asp:FileUpload ID="FileUpload1" runat="server"
Style=" top:1px; left:-10px; width: 265px; position: relative; height: 26px; opacity: 0; filter: alpha(opacity=0)"
<asp:Button ID="Button1" runat="server" Text="Choose file.."
Style="top: -5px; left:-265px; z-index: -1; width: 251px; position: relative;"
In the above code, you can see that there are two controls - one is file upload and another one is a button.
Now by giving this CSS property "
opacity: 0; filter: alpha(opacity=0)
" to file upload, we can makefile upload control as invisible.
We all know that we cannot increase the width size of browse button in File Upload control so to increase the width of the browse button, we increase the font size so the browsebutton in file upload will become a little wider.
Now shrink the fileupload until the textbox in file upload disappears. Make sure that the controls are placed inside the "
" tag and the overflow of that
tag is hidden.
Now right-click on the ASP button and give send to back (index as
By placing the ASP button over the file upload control, ASP button will be placed behind the file upload control.
So when end user clicks on that Asp button, naturally File upload control which is placed over the button will be clicked and "Choose file to upload dialogue" box will be triggered.Notes
Make some minor changes in CSS properties so that the application works perfectly on your system because your screen resolutions may vary.Reference