Problem with FTP - corrupt file at the other end

MondeoST24

Member
Joined
Mar 9, 2006
Messages
16
Programming Experience
Beginner
Hi,

I've got this code working to ftp an image

VB.NET:
Dim request As FtpWebRequest = DirectCast(WebRequest.Create("server/" + txtRef.Text + ".jpg"), FtpWebRequest) 
request.Method = WebRequestMethods.Ftp.UploadFile 
request.Credentials = New NetworkCredential("leasing", "pass") 
Dim sourceStream As New IO.StreamReader(Server.MapPath(".") + "\" + txtRef.Text + ".jpg") 
Dim fileContents As Byte() = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()) 
sourceStream.Close() 
request.ContentLength = fileContents.Length 
Dim requestStream As IO.Stream = request.GetRequestStream 
requestStream.Write(fileContents, 0, fileContents.Length) 
requestStream.Close() 
Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse) 

response.Close()

The problem is that when the image ends up on the ftp server its corrupted, it cant be opened as a jpeg?

Could this be something to do with the encoding?

Thanks
 
Trying to read the binary image file as a UTF8 encoded string is likely the source of error. Try this:
VB.NET:
Dim fileContents As Byte() = My.Computer.FileSystem.ReadAllBytes("path")
Or use a use a IO.FileStream to read and transfer smaller pieces of the file so you don't have load whole large files into memory.
 
Back
Top