OpenfileDialog in ASP.NET code behind ILLEGAL ?

H

hullnwm

In my asp.net page I'm attempting to use the OpenfileDialog object to
offer a common dialog box for the user to select a particular file on their
hard drive that may subsequently be uploaded
to our website.
The reason I'm doing it this way is that I've read that by using the common
dialog box, the security issues are based on the logged in user.

Of course, I added a reference to System.Windows.Forms and added a using
clause.

Here's the problem, when I use the following code

OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Choose File";
ofd.InitialDirectory = @"c:\" ;
ofd.Filter = "All files (*.*)|*.*|All files (*.*)|*.*" ;
ofd.FilterIndex = 1 ;
ofd.RestoreDirectory = true ;
ofd.ShowDialog();

ShowDialog() throws the following exception:

[InvalidOperationException: It is invalid to show a modal dialog or form
when the application is not running in
UserInteractive mode. Specify the ServiceNotification or DefaultDesktopOnly
style to display a notification
from a service application.]
System.Windows.Forms.CommonDialog.ShowDialog()
TestPortal1.AttachFileForm.btnBrowse_Click(Object sender, EventArgs e) in
c:\inetpub\wwwroot\testportal1\attachfileform.aspx.cs:73
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()

Any advise you can give would be greatly appreciated.

Thanks,
IpPhD
 
K

Karl Seguin

It just doesn't work.

ASP.Net only works by rendering legitimate html, css and
javascript...browsers have no idea what/how an OpenFileDialog is/works....

you need to use an HtmlFileInput...no alterantives unless you can use an ocx
or some java (not script).

Karl
 
S

Simon Harris

I think you'll find that System.Windows.Forms is only for Windows
applications - Not for ASP.Net apps.

Have a Google for u0ploading files with ASP.Net.
 
H

hullnwm

Hi Steve,
Thanks for setting me straight and thank you very much for the link. It
helped immensely.
Thanks again,
ipPhD

Steve C. Orr said:
It's really not going to work the way you're doing it. What exactly are
you trying to accomplish?
This article shows you how to allow file uploads in ASP.NET.
http://SteveOrr.net/articles/EasyUploads.aspx

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net


hullnwm said:
In my asp.net page I'm attempting to use the OpenfileDialog object to
offer a common dialog box for the user to select a particular file on
their hard drive that may subsequently be uploaded
to our website.
The reason I'm doing it this way is that I've read that by using the
common dialog box, the security issues are based on the logged in user.

Of course, I added a reference to System.Windows.Forms and added a using
clause.

Here's the problem, when I use the following code

OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Choose File";
ofd.InitialDirectory = @"c:\" ;
ofd.Filter = "All files (*.*)|*.*|All files (*.*)|*.*" ;
ofd.FilterIndex = 1 ;
ofd.RestoreDirectory = true ;
ofd.ShowDialog();

ShowDialog() throws the following exception:

[InvalidOperationException: It is invalid to show a modal dialog or form
when the application is not running in
UserInteractive mode. Specify the ServiceNotification or
DefaultDesktopOnly style to display a notification
from a service application.]
System.Windows.Forms.CommonDialog.ShowDialog()
TestPortal1.AttachFileForm.btnBrowse_Click(Object sender, EventArgs e)
in c:\inetpub\wwwroot\testportal1\attachfileform.aspx.cs:73
System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()

Any advise you can give would be greatly appreciated.

Thanks,
IpPhD
 

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