J Trahair
Well-known member
Hi all. I have a general query regarding uploading a file from within a VB.net program to an ftp area. On my computer, in a VB.net project I am developing, any file is uploaded quickly to a private ftp area to which the program has access.
One of my customers has the same application and tried to upload a file to the same ftp area, but sometimes it takes ages and times out, other times it uploads quick enough.
I know very well that the internet is a variable thing, and that different cities and even different streets in the same city have differing broadband speeds. But always, this customer can skype me or email me successfully, and can show that other uploads/downloads carried out manually happen quickly eg. downloading a file from DropBox or a photo skyped him by a friend. It seems that the only failure is for my software to upload the file as quickly as could be done on his computer using CoreFTP or similar.
The code is as follows, and always works well on my computer. The code below ensures the ftp subfolders exist then it renames the file by prefixing the file name with '_part ', it uploads it and when successfully uploaded it removes the '_part ' prefix and the job is done.
This question is not primarily about what code to use, it's about the general idea of why it is slower than opening FileZilla etc.
Any ideas? Thank you.
One of my customers has the same application and tried to upload a file to the same ftp area, but sometimes it takes ages and times out, other times it uploads quick enough.
I know very well that the internet is a variable thing, and that different cities and even different streets in the same city have differing broadband speeds. But always, this customer can skype me or email me successfully, and can show that other uploads/downloads carried out manually happen quickly eg. downloading a file from DropBox or a photo skyped him by a friend. It seems that the only failure is for my software to upload the file as quickly as could be done on his computer using CoreFTP or similar.
The code is as follows, and always works well on my computer. The code below ensures the ftp subfolders exist then it renames the file by prefixing the file name with '_part ', it uploads it and when successfully uploaded it removes the '_part ' prefix and the job is done.
This question is not primarily about what code to use, it's about the general idea of why it is slower than opening FileZilla etc.
Any ideas? Thank you.
VB.NET:
mstrFileToCopyFrom = OpenFileDialog1.FileName
mstrFileToCopyToEndsInFilenameAndTag = strPathOnlyEndsInSlash & "_part " & strFileNameOnly
mblnOverwriteFile = True ' or False
Call CopyFile()
'Process mblnErrorInCopyFiles
Try
Dim request As FtpWebRequest = WebRequest.Create("ftp://123.456.789.123/public_ftp/" & mstrFTPMainFolderName & "/" & "Prospects")
request.Credentials = New NetworkCredential("qwe123456", "passw0rd")
request.KeepAlive = True
request.Method = WebRequestMethods.Ftp.MakeDirectory
Dim response As FtpWebResponse = request.GetResponse
response.Close()
Catch ex As Exception
End Try
Try
Dim request As FtpWebRequest = WebRequest.Create("ftp://123.456.789.123/public_ftp/" & mstrFTPMainFolderName & "/" & "Prospects" & "/" & Mid$(mstrComboBox1Text, 1, 6))
request.Credentials = New NetworkCredential("qwe123456", "passw0rd")
request.KeepAlive = True
request.Method = WebRequestMethods.Ftp.MakeDirectory
Dim response As FtpWebResponse = request.GetResponse
response.Close()
Catch ex As Exception
End Try
Try
Dim request1 As FtpWebRequest = WebRequest.Create("ftp://123.456.789.123/public_ftp/" & mstrFTPMainFolderName & "/" & "Prospects" & "/" & Mid$(mstrComboBox1Text, 1, 6) & "/" & gstrSubFolderName)
request1.Credentials = New NetworkCredential("qwe123456", "passw0rd")
request1.KeepAlive = True
request1.Method = WebRequestMethods.Ftp.MakeDirectory
Dim response1 As FtpWebResponse = request1.GetResponse
response1.Close()
Catch ex As Exception
End Try
Dim request2 As FtpWebRequest = DirectCast(WebRequest.Create("ftp://123.456.789.123/public_ftp/" & mstrFTPMainFolderName & "/" & "Prospects" & "/" & Mid$(mstrComboBox1Text, 1, 6) & "/" & gstrSubFolderName & "/" & "_part " & strFileNameOnly), System.Net.FtpWebRequest)
request2.Credentials = New NetworkCredential("qwe123456", "passw0rd")
request2.KeepAlive = True
request2.Method = WebRequestMethods.Ftp.UploadFile
request2.Proxy = Nothing 'skips long lookup
request2.Timeout = 480000 'any big number
request2.ReadWriteTimeout = 480000 'any big number
Dim file() As Byte = System.IO.File.ReadAllBytes(strPathOnlyEndsInSlash & "_part " & strFileNameOnly)
Dim strz As System.IO.Stream = request2.GetRequestStream()
strz.Write(file, 0, file.Length)
strz.Close()
strz.Dispose()
Dim request3 As FtpWebRequest = DirectCast(WebRequest.Create("ftp://123.456.789.123/public_ftp/" & mstrFTPMainFolderName & "/" & "Prospects" & "/" & Mid$(mstrComboBox1Text, 1, 6) & "/" & gstrSubFolderName & "/" & "_part " & strFileNameOnly), System.Net.FtpWebRequest)
request3.Credentials = New NetworkCredential("qwe123456", "passw0rd")
request3.Method = WebRequestMethods.Ftp.Rename
request3.RenameTo() = strFileNameOnly
Dim MyResponse As System.Net.FtpWebResponse
Try
MyResponse = CType(request3.GetResponse, FtpWebResponse)
Dim MyStatusStr As String = MyResponse.StatusDescription
Dim MyStatusCode As System.Net.FtpStatusCode = MyResponse.StatusCode
If MyStatusCode <> Net.FtpStatusCode.FileActionOK Then
MsgBox("Upload " & strFileNameOnly & " failed. Try again.", vbExclamation, mCompanyName)
Else
'MessageBox.Show("Rename " & "_part " & strFileNameOnly & " to " & strFileNameOnly & " ok at " & Now.ToString)
End If
Catch ex As Exception
MsgBox("Upload " & strFileNameOnly & " failed due to the following error: " & ex.Message & ". Try again.", vbExclamation, mCompanyName)
End Try