开发者

vb.net file upload using ftp Error

开发者 https://www.devze.com 2023-03-04 18:03 出处:网络
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.

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开发者_如何学Go this line

Dim objBuffer(Convert.ToInt32(objReader.Length - 1)) As Byte

Can any one please help me.

Thanks,

Senthil


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!


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


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.


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:

  1. Read (e.g.) 64 kB from the file.
  2. Write the read 64 kB to the stream.
  3. Repeat until everything is read/written.

A quick search lead me to this example which might give you some ideas.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号