Si vous voulez simplement directement re-télécharger le fichier, il suffit de siffler le flux de téléchargement pour le flux de téléchargement:
Dim downloadRequest As FtpWebRequest =
WebRequest.Create("ftp://downloadftp.example.com/source/path/file.txt")
downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile
downloadRequest.Credentials = New NetworkCredential("username1", "password1")
Dim uploadRequest As FtpWebRequest =
WebRequest.Create("ftp://uploadftp.example.com/target/path/file.txt")
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile
uploadRequest.Credentials = New NetworkCredential("username2", "password2")
Using downloadResponse As FtpWebResponse = downloadRequest.GetResponse(),
sourceStream As Stream = downloadResponse.GetResponseStream(),
targetStream As Stream = uploadRequest.GetRequestStream()
sourceStream.CopyTo(targetStream)
End Using
Si vous avez besoin de traiter le contenu s en quelque sorte, ou si votre besoin de suivre les progrès, ou les deux, vous devez le faire morceau par morceau (ou peut-être la ligne par ligne, si elle est un fichier texte, que vous traitez):
Dim downloadRequest As FtpWebRequest =
WebRequest.Create("ftp://downloadftp.example.com/source/path/file.txt")
downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile
downloadRequest.Credentials = New NetworkCredential("username1", "password1")
Dim uploadRequest As FtpWebRequest =
WebRequest.Create("ftp://uploadftp.example.com/target/path/file.txt")
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile
uploadRequest.Credentials = New NetworkCredential("username2", "password2")
Using downloadResponse As FtpWebResponse = downloadRequest.GetResponse(),
sourceStream As Stream = downloadResponse.GetResponseStream(),
targetStream As Stream = uploadRequest.GetRequestStream()
Dim buffer As Byte() = New Byte(10240 - 1) {}
Dim read As Integer
Do
read = sourceStream.Read(buffer, 0, buffer.Length)
If read > 0 Then
' process "buffer" here
targetStream.Write(buffer, 0, read)
End If
Loop While read > 0
End Using
Voir aussi:
Je cherchais une solution similaire et trouvé un bon code par THE AMAZING. Vérifiez le lien ci-dessous: http://stackoverflow.com/questions/5938893/using-ftp-to-download-each-file-while-getting-the-file-list – GiorgiTBS