Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8885355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:17:23+00:00 2026-06-14T21:17:23+00:00

I am using the following code to upload files to a FTP, the files

  • 0

I am using the following code to upload files to a FTP, the files get uploaded – the issue is with xls, pdf and video files, they get corrupted. Txt, sql remain intact, i have checked this in the FTP directly. I am thinking its something to do with UTF8.GetBytes? Code is below

                Dim URI As String = Url & "/" & fileName
                Dim ftp As System.Net.FtpWebRequest = CType(FtpWebRequest.Create(URI), FtpWebRequest)
                ftp.Credentials = New System.Net.NetworkCredential(Username, Password)
                ftp.Proxy = Nothing
                ftp.KeepAlive = False
                ftp.UsePassive = False
                ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile

                Dim sourceStream As New StreamReader(file.InputStream)
                Dim fileContents As Byte() = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd())
                sourceStream.Close()


                ftp.ContentLength = fileContents.Length
                ftp.UsePassive = True

                Dim requestStream As Stream = ftp.GetRequestStream()
                requestStream.Write(fileContents, 0, fileContents.Length)
                requestStream.Close()

                Dim response As FtpWebResponse = DirectCast(ftp.GetResponse(), FtpWebResponse) 

Any help would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:17:24+00:00Added an answer on June 14, 2026 at 9:17 pm

    This is the problem:

    Dim fileContents As Byte() = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd())
    

    You’re reading the stream as a string, as if it’s UTF-8-encoded text, then converting it back to bytes again. Why would you do this? The files in question aren’t UTF-8-encoded text, so you shouldn’t treat them that way.

    Read binary data from the stream, never converting it into text unless you’re sure it really is text data. It’s not clear what the type of file is here, but hopefully you can get the length without reading the whole thing into memory. You can then use:

    file.InputStream.CopyTo(requestStream)
    

    to copy all the data. Note that you should use Using statements instead of manually closing streams, to avoid leaving them open if exceptions are thrown.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code to upload a pdf file through ftp : try
I'm using the following code to upload files into database the code is working
When I upload files in Yii using the following code it generates an error.
I am currently using the following code to upload images to an ftp server:
Using following code I try to get updated list of checkbuttons' corresponding text values,
I am using following code to get bitmap from url. This function is used
I am using following code to get source for image in wpf (image is
I'm trying to send a file using ftp. I have the following code: string
I am using the following code, upload.php, to try to have someone upload a
I am using the following code to rotate an uploaded jpeg image if the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.