Question : VB.NET: a correct size of stream buffer

Hi X-perts,

I am downloading a file using the code below. The dl.php script creates a header with an actual downloadable file content.

It works fine on small files but doesn't do anything on large files (20MB).

What should be a correct size of inBuf?

Am I missing some other things here?

Thanks
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim wr As HttpWebRequest = CType(WebRequest.Create("http://www.mysite.com/dl.php"), HttpWebRequest)
        Dim ws As HttpWebResponse = CType(wr.GetResponse(), HttpWebResponse)
        Dim str As Stream = ws.GetResponseStream()
        Dim inBuf(10000000) As Byte
        Dim bytesToRead As Integer = CInt(inBuf.Length)
        Dim bytesRead As Integer = 0
        While bytesToRead > 0

            Dim n As Integer = str.Read(inBuf, bytesRead, bytesToRead)

            If n = 0 Then
                Exit While
            End If
            bytesRead += n
            bytesToRead -= n
            MsgBox(bytesRead & " : " & bytesToRead)
            Me.ProgressBarAdv1.Value = Math.Round(100 * bytesRead / n, 0)
            Application.DoEvents()
        End While
        Dim fstr As New FileStream("C:\Data2\dl\ms1.zip", FileMode.OpenOrCreate, FileAccess.Write)
        fstr.Write(inBuf, 0, bytesRead)
        str.Close()
        fstr.Close()
    End Sub

Answer : VB.NET: a correct size of stream buffer

I think this line should be:

    Dim n As Integer = str.Read(inBuf, 0, bytesToRead)


For more information, take a look at the article I wrote at The Code Project:

    http://www.codeproject.com/KB/vb/WebDAV_Download.aspx

(just skip down to the 3 code section)


Random Solutions  
 
programming4us programming4us