1

I want to show up an OpenFileDialog box in my ASP .NET web application. I am writing in c#. I want to create a user account with an image so I need to select image from my computer and save it into a database. How can I implement it.

Steve B
  • 36,818
  • 21
  • 101
  • 174
snvngrc
  • 177
  • 2
  • 4
  • 12

4 Answers4

8

You can do that using

   <input type="file"  id="fileLoader" name="files" title="Load File" ...

The usual trick is to make it invisible, and on clicking some visible artifact (styled link, image, button... whatever) simulate a click on fileLoader:

 $("#fileLoader").click();
Tigran
  • 61,654
  • 8
  • 86
  • 123
3

you cannot use that Windows Forms class in ASP.NET, you should use the FileUpload class/control.

Or see other alternatives: Uploading Files in ASP.net without using the FileUpload server control

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
2

You cannot. OpenFileDialog is something for desktop applications.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
0

Almost the same as Tigran's above but I found I needed to change the JavaScript slightly to use getElementById("...") to identify the button to click(). If I just did this in HTML/CSS Tigran's code worked fine but when I used it within an .aspx file I required the change.

.aspx

<input type="file" id="fileLoader" name="files" title="Load File" />   
<asp:Button ID="LoginButton" runat="server"  Text="ASP click Me" onclientclick="openfileDialog()"  />

JavaScript

function openfileDialog() {
         document.getElementById("fileLoader").click();
     }

css

#fileLoader{
display:none;}
GWR
  • 90
  • 10