Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Upload not working over network

Status
Not open for further replies.
Jul 28, 2011
167
0
0
NG
Hi all, I have a site that has an upload page.
The idea is that users should be able to upload different files (Files are stored on the database).
I have tested from VS 2008 and everything seems to be working fine there. Now, when I upload the site to my remote server, and I test the upload there, it still works well (I can upload on the remote server).

The problem is that when I login from my local machine on the hosted site and try to upload, I get an error.
Basically, I cant upload from my local machine to the remote server.
I use thesame database (sqlserver2008) both for testing and remote hosting.
Note: remote server is another computer in the network (Intranet).
What can I do.
Code
Code:
<asp:FileUpload ID="myFile" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload New" />
code behind
Code:
  Private connectionString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString()

  Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
    If Not (myFile.PostedFile Is Nothing) Then

      Dim intFileNameLength As Integer
      Dim strFileNamePath As String
      Dim strFileNameOnly As String

      'Logic to find the FileName (excluding the path)
      strFileNamePath = myFile.PostedFile.FileName
      intFileNameLength = InStr(1, StrReverse(strFileNamePath), "\")
      strFileNameOnly = Mid(strFileNamePath, (Len(strFileNamePath) - intFileNameLength) + 2)

      'First save to Files directory
      'Dim dirPath As String = AppDomain.CurrentDomain.BaseDirectory + "Files"
      'myFile.PostedFile.SaveAs(dirPath & "\" & strFileNameOnly)

      Dim fs As FileStream = New FileStream(strFileNamePath, FileMode.Open, FileAccess.Read)
      Dim br As BinaryReader = New BinaryReader(fs)
      Dim bytes As Byte() = br.ReadBytes(Convert.ToInt32(fs.Length))

      br.Close()
      fs.Close()

      'insert the file into database

      Dim strQuery As String = "insert into Intranet_File_Table(docNo, ContentType, fileName, fileDesc,uploadDate,"
      strQuery &= "revisedDate, uploadByUser,modifiedByUser , fileBlobForm)"
      strQuery &= "values (@docNo, @ContentType,@fileName,@fileDesc,@uploadDate,@revisedDate,@uploadByUser,@modifiedByUser,@fileBlobForm)"
      Dim cmd As SqlCommand = New SqlCommand(strQuery)

      cmd.Parameters.AddWithValue("@docNo", txtDocType.Text)
      cmd.Parameters.AddWithValue("@ContentType", myFile.PostedFile.ContentType)
      cmd.Parameters.AddWithValue("@fileName", strFileNameOnly)
      cmd.Parameters.AddWithValue("@fileDesc", txtDesc.Text)
      cmd.Parameters.AddWithValue("@uploadDate", Date.Now)
      cmd.Parameters.AddWithValue("@revisedDate", Date.Now)
      cmd.Parameters.AddWithValue("@uploadByUser", My.User.Name)
      cmd.Parameters.AddWithValue("@modifiedByUser", My.User.Name)
      cmd.Parameters.AddWithValue("@fileBlobForm", bytes)

      lblMsg.Text = "File Upload Success.<br />"
      lblMsg.Text &= "Content type: " & myFile.PostedFile.ContentType & "<br />"
      lblMsg.Text &= "File size: " & CStr(myFile.PostedFile.ContentLength) & " bytes<br />"
      lblMsg.Text &= "File Name: " & strFileNameOnly

      InsertUpdateData(cmd)
      updateGridView()
    Else
      lblMsg.Text = "You did not specify a file to upload"
    End If
  End Sub

  Public Function InsertUpdateData(ByVal cmd As SqlCommand) As Boolean

    Dim strConnString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString()
    Dim con As New SqlConnection(strConnString)
    cmd.CommandType = CommandType.Text
    cmd.Connection = con
    Try
      con.Open()
      cmd.ExecuteNonQuery()

      Return True
    Catch ex As Exception
      Response.Write(ex.Message)
      Return False
    Finally
      con.Close()
      con.Dispose()
    End Try
  End Function

Help please!

____________________
Men put up a strong face just to cover their weaknesses...good!
But a smile makes them live longer....
Which would you choose?

Think about it.
 
your getting an error, but what is the error? details will be invaluable to debug this.

totally guessing... I'm going to say it's a permissions issue. this is quite common when developing locally vs deploying to a remote server. especially a remote server on a another network. if you don't need the file on disk then using the PostedFile.FileBytes will be the best option.

If you must store the file on disk, then it will be better to save the file within the website's root directory. something like [tt]root/files/[/tt]. if you try to save it outside of the directory you have to start messing with security settings for IIS and the active directory.

if the file must be stored to disk outside of the web application than have the website save the files locally. then create a windows service to monitor the same directory for new files. when a file is created, have the service move the file from the website's directory to the remote directory.
you will still need to manage security settings for the windows service, but this will be much easier than altering ISS/asp.net security.

you can also replace all the cryptic string parsing with System.IO.Path functions.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Sorry I dont know how to attach a file. However, here is the error I get over the network

Server Error in '/' Application.
--------------------------------------------------------------------------------

Could not find a part of the path 'D:\Users\ahassan\Desktop\areaMassChoir.txt'.


If you dont mind, I'd like to see a code as to how to implement a different strategy

____________________
Men put up a strong face just to cover their weaknesses...good!
But a smile makes them live longer....
Which would you choose?

Think about it.
 
ok, I've experienced this before. try using the full UNC path, rather than the mapped drive letter.
[tt]\\computer name\d$\users\ahasan\desktop\areaMassChoir.txt[/tt]
$ is only needed if the drive is not shared. you will most likely need administrative rights to do this. but try it as is and see if that resolves the problem.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thanks Jason.

I eventually worked around it with this
Code:
If myFile.PostedFile Is Nothing OrElse String.IsNullOrEmpty(myFile.PostedFile.FileName) OrElse myFile.PostedFile.InputStream Is Nothing Then
      lblMsg.Text = "You did not specify a file to upload"
    Else

      Dim intFileNameLength As Integer
      Dim strFileNamePath As String
      Dim strFileNameOnly As String

      'Logic to find the FileName (excluding the path)
      strFileNamePath = myFile.PostedFile.FileName
      intFileNameLength = InStr(1, StrReverse(strFileNamePath), "\")
      strFileNameOnly = Mid(strFileNamePath, (Len(strFileNamePath) - intFileNameLength) + 2)

      'First save to Files directory
      Dim CurrentPath As String = Server.MapPath("~/Files/")
      CurrentPath += strFileNameOnly
      myFile.SaveAs(CurrentPath)

      'Now save to database
      Using Conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Try
          Dim strQuery As String = "insert into Intranet_File_Table(docNo, ContentType, fileName, fileDesc,uploadDate,"
          strQuery &= "revisedDate, uploadByUser,modifiedByUser , fileBlobForm)"
          strQuery &= "values (@docNo, @ContentType,@fileName,@fileDesc,@uploadDate,@revisedDate,@uploadByUser,@modifiedByUser,@fileBlobForm)"

          Dim cmd As New SqlCommand(strQuery, Conn)
          cmd.Parameters.AddWithValue("@docNo", txtDocType.Text)
          cmd.Parameters.AddWithValue("@ContentType", myFile.PostedFile.ContentType)
          cmd.Parameters.AddWithValue("@fileName", strFileNameOnly)
          cmd.Parameters.AddWithValue("@fileDesc", txtDesc.Text)
          cmd.Parameters.AddWithValue("@uploadDate", Date.Now)
          cmd.Parameters.AddWithValue("@revisedDate", Date.Now)
          cmd.Parameters.AddWithValue("@uploadByUser", My.User.Name)
          cmd.Parameters.AddWithValue("@modifiedByUser", My.User.Name)

          Dim imageBytes(myFile.PostedFile.InputStream.Length) As Byte
          myFile.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length)
          cmd.Parameters.AddWithValue("@fileBlobForm", imageBytes)

          InsertUpdateData(cmd)
        Catch
          Conn.Close()
        End Try
      End Using

      lblMsg.Text = "File Upload Success.<br />"
      lblMsg.Text &= "Content type: " & myFile.PostedFile.ContentType & "<br />"
      lblMsg.Text &= "File size: " & CStr(myFile.PostedFile.ContentLength) & " bytes<br />"
      lblMsg.Text &= "File Name: " & strFileNameOnly

      updateGridView()
    End If

____________________
Men put up a strong face just to cover their weaknesses...good!
But a smile makes them live longer....
Which would you choose?

Think about it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top