Open file selection dialog from ASP.NET application on IIS

Pavel Matras 1 Reputation point
2023-02-23T13:12:09.2533333+00:00

Hello,

please I am really beginner in developing web applications. What is the best way to achieve this:

I need a web application where I press button it will open file selection dialog. There I can choose multiple files, and those filenames appears on web page in ListBox.

I am trying something like this:

using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace DmsNorway
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btFiles_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread((ThreadStart)(() =>
            {
                lbFiles.Items.Clear();
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Multiselect = true;
                ofd.Filter = "All files|*.*";
                ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    foreach (string file in ofd.FileNames)
                    {
                        lbFiles.Items.Add(Path.GetFileName(file));
                    }
                }
            }));

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
        }
    }
}

And it's working. But only when I run it directly from Visual Studio. When I deploy it to my local IIS, file selection dialog doesn't appear, and I receive this error.

User's image

It means that I have to use some client side Javascript code?

Thanks a lot for any advices.

Windows development Internet Information Services
Developer technologies ASP.NET Other
{count} votes

4 answers

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2023-02-23T13:52:07.79+00:00

    Your code, if it worked, would only open on the web server, the web application user cannot see the dialog. You can see the dialog while developing since your development machine is both the server and client.

    You want to upload the files from the browser to the server. Save the file on the web server or database or a combination. Then use a data bound server control in the code behind to display the file names.

    Use Visual C# to upload a file to a Web site


  2. SurferOnWww 4,631 Reputation points
    2023-02-24T01:03:02.03+00:00

    And it's working. But only when I run it directly from Visual Studio. When I deploy it to my local IIS, file selection dialog doesn't appear, and I receive this error.

    Please see the following Microsoft document:

    Environment.UserInteractive Property

    "The UserInteractive property reports false for a Windows process or a service like IIS that runs without a user interface. If this property is false, do not display modal dialogs or message boxes because there is no graphical user interface for the user to interact with."

    I understand that you activated the Visual Studio using your account which has a user profile in your PC, you run your ASP.NET application on IIS Express in which the worker process is executed on your account and the process is running in user interactive mode. That is why "it's working...from Visual Studio."

    Please check if the Environment.UserInteractive property is true in the .aspx.cs to confirm the above.

    Note that even if you could show the dialog it is useless as the dialog is displayed on the web server where client cannot see.


  3. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-02-24T16:45:52.41+00:00

    OpenFileDialog runs on the machine hosting the web app. Typically a service like IIS does not have access to a window to open the dialog box. if you are only going access IIS from the box running IIS, this can work with proper permissions, but typically website are accessed from remote machines.

    it a little more complex with a web application. you use the <input type="file" multiple> to open the file dialog on a browser app. the browser will then upload the files to the server on the submit. google for file upload examples.

    your server code will need to save the uploaded files, update the list and pre-render the page.

    with modern browsers you can have more control with javascript. its common, preview the file file with javascript before submit.

    0 comments No comments

  4. Lex Li (Microsoft) 6,037 Reputation points Microsoft Employee
    2023-02-24T19:06:53.8866667+00:00

    For most developers moving from console/WinForms project types to web projects, the most important things to learn are the typical differences. I wrote a lot in https://halfblood.pro/web-application-differences-in-visual-studio-and-iis-60fec7e311b3

    Why WinForms Dialog API won't work

    So, back to your case. You can feel free to call OpenFileDialog API in a web project anywhere you like, but the effect can be surprising,

    • Everything should work when hosting on IIS Express/VS, as the process runs under your account (other answers agreed) and in your user session (other answers didn't even mention). You can see the dialog and click there.
    • However, when hosting on full IIS, the worker process is in session 0 (please study Windows session isolation) so is the dialog. As you cannot see into that session by default, the exception occurs. Even if you dismiss the interactive exception, the dialog is showed on the session 0 desktop, which is invisible to you and you cannot click it.

    Thus, the web app hangs there forever unless you kill the process.

    OK. Enough background, but that explains why everyone says you shouldn't use OpenFileDialog or any similar thing in a web project.

    Note that you shouldn't use other WinForms API either, like System.Drawing.

    What exactly should you use

    I think you just start to learn the web, but the modern web is now so vulnerable that in most cases a web app shouldn't attempt to touch users' local file system.

    Therefore, your web app should first consider using a cloud based file storage API to help users read/write files. OneDrive/Dropbox/iCloud/Azure Files and others are all popular.

    When you have to touch local file system through browser sandbox boundary, there are modern browser side API to pop up the dialogs, such as Window.showSaveFilePicker()

    https://developer.mozilla.org/en-US/docs/Web/API/Window/showSaveFilePicker

    However, consuming such browser API requires a modern web app type and ASP.NET Core/Blazor makes it rather simple),

    https://learn.microsoft.com/en-us/aspnet/core/blazor/file-uploads?view=aspnetcore-7.0&pivots=server

    You can of course find ways to do similar with the legacy ASP.NET WebForms you chose, but in the long run your investment isn't worth the while.

    Probably too much for you to digest. Find a local teacher/experienced consultant to guide you through, and that can save you a lot of time in the end.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.