FtpWebRequest downloads

T

ThatsIT.net.au

I recently made a ftp upload, and now I am trying to make a ftp download, my
problem is the file is always corrupted. I can upload the file no
problems, I can download text files, but I cant download images. I have a
217 kb png image, when I download it it is corrupted and it is 391 kb in
size.

any ideas

Private Sub downloadFileFromFTP(ByVal fileName)
Dim ftpUrl As Uri = New Uri("ftp://192.168.0.2/" & fileName)
Try
Dim ftpReq As FtpWebRequest =
WebRequest.Create(ftpUrl.AbsoluteUri)
ftpReq.Proxy = Nothing

ftpReq.Method = WebRequestMethods.Ftp.DownloadFile
ftpReq.Credentials = New NetworkCredential("au\mosley",
"faron&missy")
ftpReq.UseBinary = True
Dim ftpResp As FtpWebResponse = ftpReq.GetResponse
Dim ftpRespStream As Stream = ftpResp.GetResponseStream

Dim reader As StreamReader
reader = New StreamReader(ftpRespStream, True)
Dim sw As StreamWriter = New StreamWriter("c:\test\ftp\" &
fileName)
sw.AutoFlush = True
sw.Write(reader.ReadToEnd)
reader.Close()
sw.Close()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
 
A

Arne Vajhøj

ThatsIT.net.au said:
I recently made a ftp upload, and now I am trying to make a ftp
download, my
problem is the file is always corrupted. I can upload the file no
problems, I can download text files, but I cant download images. I have a
217 kb png image, when I download it it is corrupted and it is 391 kb in
size.
ftpReq.UseBinary = True
Dim ftpResp As FtpWebResponse = ftpReq.GetResponse
Dim ftpRespStream As Stream = ftpResp.GetResponseStream

Dim reader As StreamReader
reader = New StreamReader(ftpRespStream, True)
Dim sw As StreamWriter = New StreamWriter("c:\test\ftp\" &
fileName)
sw.AutoFlush = True
sw.Write(reader.ReadToEnd)
reader.Close()
sw.Close()

StramReader and StreamWriter are for text.

For binary use Stream only for both from and to.

Arne
 
T

ThatsIT.net.au

Thank you I have been pulling my hiar ouut over this, I used stream for my
upload code below and even after looking though both upload and download
code over and over i never noticed the difference. Thanks for pointing this
out while i still have some hair left


Public Sub uploadFile()
Dim ftpUrl As Uri = New Uri("ftp://192.168.0.2/" & fileName)
Dim ftpReq As FtpWebRequest
Dim ftpResp As FtpWebResponse
Try
ftpReq = FtpWebRequest.Create(ftpUrl)
ftpReq.UseBinary = True
ftpReq.Method = WebRequestMethods.Ftp.UploadFile
ftpReq.Credentials = New NetworkCredential("au\mosley",
"faron&missy")
ftpReq.Proxy = Nothing
Dim f As FileInfo = New FileInfo("c:\test\" & fileName)
Dim Contents As Byte() = New Byte(f.Length) {}
Dim fs As FileStream = f.OpenRead
fs.Read(Contents, 0, f.Length)
fs.Close()
FtpWebRequest.DefaultWebProxy.Credentials = New
NetworkCredential("au\mosley", "faron&missy")

Dim rs As Stream = ftpReq.GetRequestStream()
rs.Write(Contents, 0, f.Length)
rs.Flush()
rs.Close()

ftpResp = ftpReq.GetResponse
Console.WriteLine(ftpResp.WelcomeMessage)
Console.WriteLine(ftpResp.BannerMessage)
Console.WriteLine("Code: {0} Description: {1}",
ftpResp.StatusCode, ftpResp.StatusDescription)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try

End Sub
 
A

Arne Vajhøj

ThatsIT.net.au said:
Dim Contents As Byte() = New Byte(f.Length) {}
Dim fs As FileStream = f.OpenRead
fs.Read(Contents, 0, f.Length)
fs.Close()
Dim rs As Stream = ftpReq.GetRequestStream()
rs.Write(Contents, 0, f.Length)
rs.Flush()


I am not too happy about that Read above. I think you should
put it in a while loop with Contents of a fixed size.

Two reasons:
1) Read may not actually read all the bytes in a single read
even if they are there (*).
2) You will have a problem with a 5 GB file.

*)
http://msdn.microsoft.com/en-us/library/system.io.filestream.read.aspx
says:
An implementation is free to return fewer bytes than requested even
if the end of the stream has not been reached.

Arne
 
T

ThatsIT.net.au

Arne Vajhøj said:
I am not too happy about that Read above. I think you should
put it in a while loop with Contents of a fixed size.

Two reasons:
1) Read may not actually read all the bytes in a single read
even if they are there (*).
2) You will have a problem with a 5 GB file.


yep, I have a loop now
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top