0

I am currently trying to open a file on my web app by retrieving the file path from a sql database

here is my code that will open the file when I double click an index in a listbox.

Protected Sub userdoubleClick()
    Dim selectedPath As String

    Try
        fileConnection.Open()


        selectedFile = FileListBox.SelectedValue

        'takes the selected items from the listbox and searches the database for the file 
        'and will open the file for the user
        fileCommand.CommandText = "SELECT filePath FROM ImportedFiles WHERE FileName = '" & selectedFile & "'"

        fileDataReader = fileCommand.ExecuteReader
        Do While fileDataReader.Read
            selectedPath = fileDataReader.GetValue(0)
        Loop

        System.Diagnostics.Process.Start(selectedPath)

    Catch ex As Exception

    End Try

    fileConnection.Close()


End Sub

When I run this on my local PC it works fine but then when I publish it to a server it wont open the file.

averyto8
  • 45
  • 7
  • What path are you talking about? A path on your local machine? A path relative to the root of your site? Please provide an example – Steve Jul 10 '15 at 17:30
  • The file path is to a share drive and will look something like this \\webapp\backup\Archived\20150709_1324849.pdf – averyto8 Jul 10 '15 at 17:33
  • What are you trying to do with the file once you open it? Are you returning the contents to a browser? Are you processing the file? Also, when you say it "won't open" what do you mean? Are you getting an exception? I'd wager the exception is a permissions problem, especially if the file is on a shared server. – Bob Mc Jul 10 '15 at 21:46
  • All I'm having them do is view the documents stored in the share drive. I believe the user should have access to the drive, I have a few lines of code with a dummy account that has access to the share drive – averyto8 Jul 13 '15 at 12:08
  • From your reply I'm pretty certain that the problem is permissions. If you can verify that by telling me what exception you're getting I can point you in the correct direction. – Bob Mc Jul 13 '15 at 13:53
  • When I attempt to download the pdf it starts the process of downloading but after around 30 seconds it says "failed - network error" when looking at my downloads in my browser (chrome) – averyto8 Jul 13 '15 at 14:28
  • That error is just a generic Chrome error that's masking your actual problem. You're not getting the _true_ exception because your `Catch` handler is not logging it anywhere. I suggest using [log4net](https://logging.apache.org/log4net/) or even writing to a text file within your `Catch` handler to get the actual cause of the error. – Bob Mc Jul 13 '15 at 18:50

2 Answers2

2

System.Diagnostics.Process.Start(selectedPath) will open the file on the machine running the web application; in other words, it is opening it on your local machine when running locally, but opening on the server when deployed. This just isn't going to work.

See ASP.Net Download file to client browser

Community
  • 1
  • 1
Paul Abbott
  • 7,065
  • 3
  • 27
  • 45
1

If you're accessing the files from a web application, then the user identity under which the file is opened is not that of the user logged into your application. It's the user under which the IIS application pool is running. That's the user that needs to have permission granted to your shared drive.

There are many solutions to this problem, including changing the context under which the application pool is running or making the files local.

Bob Mc
  • 1,980
  • 1
  • 28
  • 38