Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / July 2007

Tip: Looking for answers? Try searching our database.

OpenFile Dialog in ASP.Net

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Umeshnath - 06 Jul 2007 13:28 GMT
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
Mr. Arnold - 06 Jul 2007 15:15 GMT
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.
Matt Lacey - 06 Jul 2007 15:29 GMT
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.
Herfried K. Wagner [MVP] - 06 Jul 2007 18:03 GMT
"Matt Lacey" <m.lacey@fdsltd.co.uk> schrieb:
> 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.

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>

Norman Yuan - 06 Jul 2007 15:30 GMT
>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
[quoted text clipped - 17 lines]
>            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
>
[quoted text clipped - 9 lines]
>
> 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).

> Thanks in advance Umeshnath

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.