帮助我在VB.NET中恢复FTP代码 [英] Help with my Resume FTP code in VB.NET

查看:59
本文介绍了帮助我在VB.NET中恢复FTP代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我真的很想让它工作.

我的问题是我无法使文件恢复到上次中断的位置.

它会继续,但是会从头开始添加(这是我的假设),然后添加到现有文件中.

我的问题是我需要在代码中进行哪些更改以解决此问题?我下面的箭头指向的部分是我认为有问题的位置.

我尝试在查看部分文件并获得文件大小的位置添加偏移量,但除0以外的任何值都不适合我.任何帮助将是巨大的.谢谢.

Hi,

I am so close to getting this to work.

My problem is that I can''t get the file to resume where it left off.

It will resume but then it will append from the beginning (that is my assumption) and append to the existing file.

My question is what do I need to change in my code to fix that? The part below where I have an arrow pointing to is where I think I have the issue.

I tried to add an offset where I looked at the partial file and got the file size but any value other than 0 doesn''t work there for me. Any help would be great. Thanks.

Private Sub backgroundWorker4_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles backgroundWorker4.DoWork

        Try
            Dim URI As String = host & remoteFile
            Dim ftp As System.Net.FtpWebRequest = CType(FtpWebRequest.Create(URI), FtpWebRequest)
            ftp.Credentials = New System.Net.NetworkCredential(username, password)
            ftp.UseBinary = True
            ftp.KeepAlive = False


            Dim SavePathStreamReader As New StreamReader("C:\Program Files\Download Manager\SavePath.txt")
            Dim SavePath As String = SavePathStreamReader.ReadLine()

            'Define the action required (in this case, download a file)
            ftp.Method = Net.WebRequestMethods.Ftp.DownloadFile

            'Get the response to the Ftp request and the associated stream
            Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, System.Net.FtpWebResponse)
                Using responseStream As IO.Stream = response.GetResponseStream
                    'loop to read & write to file

                    Using fs As New IO.FileStream(SavePath, IO.FileMode.Append)

                        Dim buffer(2047) As Byte
                        Dim read As Integer = 0
                        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)

                        Dim readings As Integer
                        Dim safedelegate As New ChangeTextsSafe(AddressOf ChangeTexts)
                        Dim safedelegate2 As New ChangeTextsSafe(AddressOf ChangeTexts2)

                        Do
This Is where I   ----->    read = responseStream.Read(buffer, 0, buffer.Length)
think my problem is         fs.Write(buffer, 0, read)
                            If readings = 750 Then
                                Dim MyFile As New FileInfo(localFile)
                                Dim length As Long = MyFile.Length
                                Dim TextUpdate As Integer
                                TextUpdate += 1
                                Me.Invoke(safedelegate, length) 'Invoke the TreadsafeDelegate
                                Me.Invoke(safedelegate2, length) 'Invoke the TreadsafeDelegate
                                readings += 1
                            ElseIf readings = 751 Then
                                readings = 0
                            Else
                                readings += 1
                            End If
                        Loop Until (read = 0 Or worker.CancellationPending)
                        responseStream.Close()
                        fs.Flush()
                        fs.Close()
                    End Using
                    responseStream.Close()
                End Using
                response.Close()
            End Using

        Catch Cancelex As Exception
            Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
            If worker.CancellationPending Then
                jobCancel = 1
            Else
                jobCancel = 2
            End If
        End Try
    End Sub

推荐答案

我想出了我自己问题的答案.我需要将起点添加到FTP服务器,因此需要在downloadfile调用之后添加此代码,以便添加该行,然后下一行是我用来修复该行的内容:

ftp.Method = Net.WebRequestMethods.Ftp.DownloadFile
ftp.ContentOffset = ResumeDL<-ResumeDL是一个整数变量,可通过单击恢复按钮来填充,并获取部分下载的文件的剩余大小...换句话说,这是偏移量变量.
I figured out the answer to my own question. I needed to add the starting point to the FTP server so I needed to add this code after the downloadfile call so I will add that line and then the next line is what I used to fix it:

ftp.Method = Net.WebRequestMethods.Ftp.DownloadFile
ftp.ContentOffset = ResumeDL <--ResumeDL is a integer variable that gets filled from on a resume button click and gets the remaining size of the partially downloaded file...this is the offset variable in other words.


这篇关于帮助我在VB.NET中恢复FTP代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆