Search code examples
asp.netmultithreadingopenfiledialog

Used a thread to get file name/path, but can't get the page to automatically update when the dialog closes


My web application needs to be able to accept the FILE NAME/PATH to a file - the file will be on a network resource which is available to both the client machine - and to the web server.

In a prior thread, Rajkumar Reddy, suggested this strategy:

Add windows openfile dialog reference toweb application by righ click on solution explore >project name add reference system.windows.forms then follow this coding style here i have >given vb sample code you can convert this to c# if your facing any problem tell me.

Import system.threading

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click

    Dim objThread As New Thread(AddressOf FnOpenFileDialog)
    objThread.IsBackground = True
    objThread.SetApartmentState(ApartmentState.STA)
    objThread.Start()
End Sub

Private Sub FnOpenFileDialog()
    Dim openfile As New System.Windows.Forms.OpenFileDialog
    'openfile.InitializeLifetimeService()
    'openfile.Filter = String.Format("Image file (*.jpg)|*.jpg|All files (*.*)|*.*")
    openfile.Filter = String.Format("Image file (*.jpg)|*.jpg")
    openfile.Multiselect = True
    openfile.ShowDialog()
End Sub

I added these two subs to a aspx page - and I can get the openfiledialog to open. I can get the file name/path into a shared variable. However, I cannot get the page to automatically update with the returned information.

I have more code I can post - but, I'm not really sure which part isn't working.


Solution

  • That won't work at all.

    Showing the file dialog in a web application only works when the web server and the web client is the same computer, i.e. only when you are testing the web application locally on your own computer.

    When you deploy the web application on the server, the dialog would open on the web server, not the computer surfing to the server, but the web server doesn't have a user interface where it can show the dialog.

    Also, it doesn't work even when you run the web application locally. As you are starting a new thread for the dialog, the web server contines to complete the page and send it to the browser. When you have selected a file, the Page object isn't there any more. The response has already been sent to the browser, so it's too late to change it.