Reputation: 13
I am using vb.net(.Net 4.0) to upload the File to server using ftp.
i could upload file small size(10MB,) without any error. but when i Try for uploading more than 2GB size it lead the following Error
System.OverflowException: Value was either too large or too small for an Int32.
I am using the code..
Private Function UploadFileToServer(ByVal sSourceFile As String, ByVal sTargetFile As String) As Boolean
Dim objCredential As NetworkCredential
Dim objRequest As FtpWebRequest
Dim objReader As FileStream
Dim objStream As Stream
Dim objResponse As FtpWebResponse
Dim bResult As Boolean = False
Try
objRequest = DirectCast(WebRequest.Create(sTargetFile), FtpWebRequest)
'objRequest = FtpWebRequest.Create(sTargetFile)
objRequest.Method = WebRequestMethods.Ftp.UploadFile
objCredential = New NetworkCredential(USER_NAME, PASSWORD)
objRequest.Credentials = objCredential
objReader = New FileStream(sSourceFile, FileMode.Open)
Dim objBuffer(Convert.ToInt32(objReader.Length - 1)) As Byte
objReader.Read(objBuffer, 0, objBuffer.Length)
objReader.Close()
objRequest.ContentLength = objBuffer.Length
objStream = objRequest.GetRequestStream()
objStream.Write(objBuffer, 0, objBuffer.Length)
objStream.Close()
objResponse = DirectCast(objRequest.GetResponse, FtpWebResponse)
objResponse.Close()
bResult = True
Catch ex As Exception
End Try
Return bResult
End Function
it shows the error on this line
Dim objBuffer(Convert.ToInt32(objReader.Length - 1)) As Byte
Can any one please help me.
Thanks,
Senthil
Upvotes: 1
Views: 1876
Reputation:
Function "UploadFileToServer" fail to upload file that has large file size >50MB. The problem is when you upload a large file that would take a long time then the connection to the port is dropped - kind like time out. I have the code fixed and can upload file size up to 200GB. Trick to make it works is you need to creat a loop to upload one byte at each iteration and need to check if the connection to the port is stil valid, if not then you need to re-establish the connection to the port and then try to append remaining bytes - not to write bytes to the file.
Change objRequest.Method = WebRequestMethods.Ftp.UploadFile
to objRequest.Method = WebRequestMethods.Ftp.AppendFile
right after the connection drop.
I've tried other effords fail.
Upvotes: 0
Reputation: 40756
Update:
As Will A pointed out, 2 GB is too large for an array. As he suggested, I would also do a chunked read, roughly like that:
A quick search lead me to this example which might give you some ideas.
Upvotes: -1
Reputation: 25008
2^31 (number of bytes in 2GB) is too large a value to store in an Integer. Creating a 2GB Byte array is verging on creating an unuseable application - what you should instead do is pick a reasonable size for your buffer and loop, performing a objStream.Write
for each chunk of data in the buffer. Short answer - don't load the entire file into memory when FTP'ing it!
Upvotes: 3
Reputation: 5330
This error occurs because you are converting to int32
Dim objBuffer(Convert.ToInt32(objReader.Length - 1)) As Byte
use int64 instead and there won't be any problem
Dim objBuffer(Convert.ToInt64(objReader.Length - 1)) As Byte
Upvotes: 0