help with FtpWebRequest

Discussion in 'ASP .Net' started by brian, Mar 6, 2009.

  1. brian

    brian Guest

    The following code fails when, and only when the transmitted file is larger than 64K. Any ideas what is wrong. The files does send but any file larger than 64k gets truncated to exactly 64KB.

    No exception occurs. If I send the file using an FTP client lile FileZilla, the file sends correctly. Point being it does seem it's a problem in the code.

    If there is a better group for this please advise.

    Thanks

    Public Sub uploadFileUsingFTP(ByVal CompleteFTPPath As String, ByVal CompleteLocalPath As String, Optional ByVal UName As String = "", Optional ByVal PWD As String = "")
    'This fuction uses four parameters,
    ' CompleteFTPPath and filename of remote site
    ' CompleteLocalPath and filename on local site
    ' Third and Fourth parameters are UName and PWD used to validate network crediential.

    Try
    Dim reqObj As FtpWebRequest = WebRequest.Create(CompleteFTPPath)

    reqObj.Method = WebRequestMethods.Ftp.UploadFile
    reqObj.KeepAlive = True
    reqObj.Credentials = New NetworkCredential(UName, PWD)
    reqObj.Proxy = New WebProxy()
    reqObj.UseBinary = True

    'FileStream object read file from Local Drive
    Dim streamObj As FileStream = File.OpenRead(CompleteLocalPath)

    'Store File in Buffer
    Dim buffer(streamObj.Length) As Byte

    'Read File from Buffer
    streamObj.Read(buffer, 0, buffer.LongLength - 1)

    'Close FileStream Object Set its Value to nothing
    streamObj.Close()
    streamObj = Nothing

    'Upload File to ftp://localHost/ set its object to nothing
    reqObj.GetRequestStream().Write(buffer, 0, buffer.LongLength - 1)
    reqObj = Nothing

    Catch ex As Exception
    tbmsg.Text = ex.Message

    End Try

    End Sub


    --------------= Posted using GrabIt =----------------
    ------= Binary Usenet downloading made easy =---------
    -= Get GrabIt for free from http://www.shemes.com/ =-
     
    brian, Mar 6, 2009
    #1
    1. Advertising

  2. brian

    bruce barker Guest

    because you only read the first 64k (default buffer size) of the file.
    you should loop reading and writing.

    -- bruce (sqlwork.com)

    brian wrote:
    > The following code fails when, and only when the transmitted file is larger than 64K. Any ideas what is wrong. The files does send but any file larger than 64k gets truncated to exactly 64KB.
    >
    > No exception occurs. If I send the file using an FTP client lile FileZilla, the file sends correctly. Point being it does seem it's a problem in the code.
    >
    > If there is a better group for this please advise.
    >
    > Thanks
    >
    > Public Sub uploadFileUsingFTP(ByVal CompleteFTPPath As String, ByVal CompleteLocalPath As String, Optional ByVal UName As String = "", Optional ByVal PWD As String = "")
    > 'This fuction uses four parameters,
    > ' CompleteFTPPath and filename of remote site
    > ' CompleteLocalPath and filename on local site
    > ' Third and Fourth parameters are UName and PWD used to validate network crediential.
    >
    > Try
    > Dim reqObj As FtpWebRequest = WebRequest.Create(CompleteFTPPath)
    >
    > reqObj.Method = WebRequestMethods.Ftp.UploadFile
    > reqObj.KeepAlive = True
    > reqObj.Credentials = New NetworkCredential(UName, PWD)
    > reqObj.Proxy = New WebProxy()
    > reqObj.UseBinary = True
    >
    > 'FileStream object read file from Local Drive
    > Dim streamObj As FileStream = File.OpenRead(CompleteLocalPath)
    >
    > 'Store File in Buffer
    > Dim buffer(streamObj.Length) As Byte
    >
    > 'Read File from Buffer
    > streamObj.Read(buffer, 0, buffer.LongLength - 1)
    >
    > 'Close FileStream Object Set its Value to nothing
    > streamObj.Close()
    > streamObj = Nothing
    >
    > 'Upload File to ftp://localHost/ set its object to nothing
    > reqObj.GetRequestStream().Write(buffer, 0, buffer.LongLength - 1)
    > reqObj = Nothing
    >
    > Catch ex As Exception
    > tbmsg.Text = ex.Message
    >
    > End Try
    >
    > End Sub
    >
    >
    > --------------= Posted using GrabIt =----------------
    > ------= Binary Usenet downloading made easy =---------
    > -= Get GrabIt for free from http://www.shemes.com/ =-
    >
     
    bruce barker, Mar 7, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darqer
    Replies:
    1
    Views:
    1,335
    GinaM
    Nov 6, 2006
  2. Replies:
    2
    Views:
    549
  3. =?Utf-8?B?QmlsbA==?=

    Passing DefaultCredentials to an FtpWebRequest

    =?Utf-8?B?QmlsbA==?=, Mar 21, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    385
    =?Utf-8?B?QmlsbA==?=
    Mar 21, 2007
  4. BizWorld

    FTPWebRequest failing - URGENT

    BizWorld, Jun 15, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    1,489
    BizWorld
    Jun 15, 2007
  5. Dan
    Replies:
    0
    Views:
    720
Loading...

Share This Page