OpenFile Dialog in ASP.Net

G

Guest

I want to open a file using OpenFile Dialog class (System.Windows.Forms) in
my ASP.NET application . I don’t want to use file upload control why because
I will not any events from File upload control after selecting the file. I
have to click a button for firing some code in file upload control.

My functionality is by selecting an image it has to load in a Image Control

I have refer the classs System.Windows.Forms nad used the below code

OpenFileDialog of = new OpenFileDialog();
of.Title = "Select a image";
of.InitialDirectory = @"C:\";
of.Filter = " Image files(*.Jpeg)|*.Jpeg";
of.RestoreDirectory = true;

if (of.ShowDialog() == DialogResult.OK)
{
Image1.ImageUrl = of.FileName.ToString();
}

It is working fine my system and when the same code runs in a different
computer I am getting the below error

“Current thread must be set to single thread apartment (STA) mode before OLE
calls can be made. Ensure that your Main function has STAThreadAttribute
marked on it. This exception is only raised if a debugger is attached to the
process.â€


Also in my system the the dialog box is coming behind of the browser , it
need to come infornt of the browser ( like in File upload)

Can any one suggest the solution for this.

Thanks in advance Umeshnath
 
M

Mr. Arnold

System.Web.UI.HtmlControls.HtmlInputFile

Asp.net File Upload.

The fact that you're using a control that's meant for a Windows form client
application in the middle of a Web form application is questionable.
 
M

Matt Lacey

Where is your code running?
Is it on the server?
Perhaps that's why it can't see the C drive of the users machine and
are gettign errors

Do not use Windows controls on a web page. If they work at all, it
will not be as you expect. Hence the way the dialog is being
displayed. They are not designed to work on web pages. That is why
there is a completely different set of controls for web pages.

Directly linking to a local resource on a webpage is definitely a bad
practice with all sorts of security holes. Browsers and languages
(client side) will stop most access to most files for security/privacy
reasons.

The file upload control is the way to get local file path names on a
web page. You don't have to upload the file, but you can't do much
else with it.

If you want to display a locally held image on a web page it must be
first uploaded to a site.
 
N

Norman Yuan

Umeshnath said:
I want to open a file using OpenFile Dialog class (System.Windows.Forms) in
my ASP.NET application . I don't want to use file upload control why
because
I will not any events from File upload control after selecting the file. I
have to click a button for firing some code in file upload control.

My functionality is by selecting an image it has to load in a Image
Control

I have refer the classs System.Windows.Forms nad used the below code

OpenFileDialog of = new OpenFileDialog();
of.Title = "Select a image";
of.InitialDirectory = @"C:\";
of.Filter = " Image files(*.Jpeg)|*.Jpeg";
of.RestoreDirectory = true;

if (of.ShowDialog() == DialogResult.OK)
{
Image1.ImageUrl = of.FileName.ToString();
}


Where do you run the code? server side or client side? Since you use C#, I
assmue it is server side code (ASP.NET code). So, you actually show the
OpenFile dialog on the server.
It is working fine my system and when the same code runs in a different


Works fine on your computer? I'd bet you are testing youe ASP.NET app on
your local IIS and the OpenFile dialog is opened by the IIS, not your
browser. Try to test your ASP.NET app from other computer's browser (that
is, the IIS server and browser on different computer) to see what happens.


computer I am getting the below error

"Current thread must be set to single thread apartment (STA) mode before
OLE
calls can be made. Ensure that your Main function has STAThreadAttribute
marked on it. This exception is only raised if a debugger is attached to
the
process."


Also in my system the the dialog box is coming behind of the browser , it
need to come infornt of the browser ( like in File upload)

Can any one suggest the solution for this.


The suggestion is No, you cannot use OpenFile dialog on the server side
(even you can, your ASP.NET users on the other side of town/world, have to
rush into the server room, find the server's monitor (the server may not
have one!) and pick a file (on the server!) and click OK, rush back to his
computer (maybe days later) and continue.

If you really want to replace the File Upload dialog on the client side with
something more customized, you can make a Win Form User Control to wrap up
the OpenFile dialog inside and embed it into the ASP.NET page as
<Object>...</Object>, similar to use ActiveX Controls in web page. However,
in this case, user must have .NET framework installed and the Browser has to
be set to allow download the code and install it (user has to answer some
security questions he possibly does not understand at all before it can be
installed. Chances are the installation be stopped).
 
H

Herfried K. Wagner [MVP]

Matt Lacey said:
Where is your code running?
Is it on the server?
Perhaps that's why it can't see the C drive of the users machine and
are gettign errors

Do not use Windows controls on a web page.

I agree for Web sites, but for an intranet site, this may be appropriate in
certain scenarios. Nevertheless, you cannot use the classes without any
problems directly in your code because it is executed on the server and not
on the client. Thus the dialog would (in theory) be shown on the server.
What can be done is developing a Windows Forms Browser Control that hosts
the OpenFileDialog and is used to show it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top