A better solution please (System.IO.Stream related) 
Author Message
 A better solution please (System.IO.Stream related)

Hi,

I want to download a file,
To do this I have the following code.

The code works, but isn't really good.
1 - I can't find the size of the file I'm about to download (Seek is not
allowed it says)
2 - I don't know when the download is finished, so I have a poor test to see
if the download is completed

What do I need to change in the following code ?
Thnx in Advance.
TP.

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim objWebRequest As WebRequest

Dim objWebResponse As WebResponse

Dim objStream As Stream

Dim objFileStream As FileStream

Dim objBinaryReader As BinaryReader

Dim objBinaryWriter As BinaryWriter

objWebResponse =
objWebRequest.Create(" http://www.*-*-*.com/ ~xgboomer/XGJK2.exe").Ge
tResponse

objStream = objWebResponse.GetResponseStream

objBinaryReader = New BinaryReader(objStream)

objFileStream = New FileStream("c:\out.exe", FileMode.Create)

objBinaryWriter = New BinaryWriter(objFileStream)

Dim blnError As Boolean = False

Dim i As Integer

Do Until blnError

Try

i = objBinaryWriter.BaseStream.Length

objBinaryWriter.Write(objBinaryReader.ReadBytes(512))

If i = objBinaryWriter.BaseStream.Length Then blnError = True 'EOF

Catch

blnError = True

End Try

Loop

MsgBox("File downloaded")

objBinaryWriter.Close()

objBinaryReader.Close()

End Sub



Mon, 20 Dec 2004 03:56:50 GMT  
 A better solution please (System.IO.Stream related)
Try using a buffered stream that way you get the size of the stream.

--
Dilip Krishnan
Architect
Vivare Inc.
(214) 282 3982


Quote:
> Hi,

> I want to download a file,
> To do this I have the following code.

> The code works, but isn't really good.
> 1 - I can't find the size of the file I'm about to download (Seek is not
> allowed it says)
> 2 - I don't know when the download is finished, so I have a poor test to
see
> if the download is completed

> What do I need to change in the following code ?
> Thnx in Advance.
> TP.

> Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load

> Dim objWebRequest As WebRequest

> Dim objWebResponse As WebResponse

> Dim objStream As Stream

> Dim objFileStream As FileStream

> Dim objBinaryReader As BinaryReader

> Dim objBinaryWriter As BinaryWriter

> objWebResponse =

objWebRequest.Create("http://home.planetinternet.be/~xgboomer/XGJK2.exe").Ge

- Show quoted text -

Quote:
> tResponse

> objStream = objWebResponse.GetResponseStream

> objBinaryReader = New BinaryReader(objStream)

> objFileStream = New FileStream("c:\out.exe", FileMode.Create)

> objBinaryWriter = New BinaryWriter(objFileStream)

> Dim blnError As Boolean = False

> Dim i As Integer

> Do Until blnError

> Try

> i = objBinaryWriter.BaseStream.Length

> objBinaryWriter.Write(objBinaryReader.ReadBytes(512))

> If i = objBinaryWriter.BaseStream.Length Then blnError = True 'EOF

> Catch

> blnError = True

> End Try

> Loop

> MsgBox("File downloaded")

> objBinaryWriter.Close()

> objBinaryReader.Close()

> End Sub



Mon, 20 Dec 2004 04:25:42 GMT  
 A better solution please (System.IO.Stream related)
You can get the size of the file you are downloading from
WebResponse.ContentLength. You can hook up a call back routine to the call
to download to handle async downloads. The sample code
http://samples.gotdotnet.com/quickstart/util/srcview.aspx?path=%2fqui...
%2fhowto%2fsamples%2fnet%2fASPXNet%2fwebrequestasync.src in illustrates this
approach.

-Madhan Subhas
VB.net

--
This posting is provided "AS IS" with no warranties, and confers no rights.


Quote:
> Hi,

> I want to download a file,
> To do this I have the following code.

> The code works, but isn't really good.
> 1 - I can't find the size of the file I'm about to download (Seek is not
> allowed it says)
> 2 - I don't know when the download is finished, so I have a poor test to
see
> if the download is completed

> What do I need to change in the following code ?
> Thnx in Advance.
> TP.

> Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load

> Dim objWebRequest As WebRequest

> Dim objWebResponse As WebResponse

> Dim objStream As Stream

> Dim objFileStream As FileStream

> Dim objBinaryReader As BinaryReader

> Dim objBinaryWriter As BinaryWriter

> objWebResponse =

objWebRequest.Create("http://home.planetinternet.be/~xgboomer/XGJK2.exe").Ge

- Show quoted text -

Quote:
> tResponse

> objStream = objWebResponse.GetResponseStream

> objBinaryReader = New BinaryReader(objStream)

> objFileStream = New FileStream("c:\out.exe", FileMode.Create)

> objBinaryWriter = New BinaryWriter(objFileStream)

> Dim blnError As Boolean = False

> Dim i As Integer

> Do Until blnError

> Try

> i = objBinaryWriter.BaseStream.Length

> objBinaryWriter.Write(objBinaryReader.ReadBytes(512))

> If i = objBinaryWriter.BaseStream.Length Then blnError = True 'EOF

> Catch

> blnError = True

> End Try

> Loop

> MsgBox("File downloaded")

> objBinaryWriter.Close()

> objBinaryReader.Close()

> End Sub



Mon, 20 Dec 2004 04:49:00 GMT  
 A better solution please (System.IO.Stream related)
Hi,

This code seems to work fine, but it might fail if the file is exactly as
large as (or a multiple of)  the buffer since you will get one read when
there is nothing more to read, but I'll leave that to you :-) :

Dim objWebRequest As WebRequest
Dim objWebResponse As WebResponse
Dim objFileStream As FileStream
Dim objBufferedStream As BufferedStream
Dim b(8192) As Byte
Dim iRead As Integer

objWebResponse =
objWebRequest.Create("http://home.planetinternet.be/~xgboomer/XGJK2.exe").Ge
tResponse()
objBufferedStream = New BufferedStream(objWebResponse.GetResponseStream)
objFileStream = New FileStream("c:\out.exe", FileMode.Create)

Do
    iRead = objBufferedStream.Read(b, 0, b.Length)
    objFileStream.Write(b, 0, b.Length)
Loop While iRead > 0

MsgBox("File downloaded")
objBufferedStream.Close()
objFileStream.Close()

Good luck!

/Per



Mon, 20 Dec 2004 04:53:32 GMT  
 A better solution please (System.IO.Stream related)
Thnx for all your replys



Quote:
> Hi,

> I want to download a file,
> To do this I have the following code.

> The code works, but isn't really good.
> 1 - I can't find the size of the file I'm about to download (Seek is not
> allowed it says)
> 2 - I don't know when the download is finished, so I have a poor test to
see
> if the download is completed

> What do I need to change in the following code ?
> Thnx in Advance.
> TP.

> Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load

> Dim objWebRequest As WebRequest

> Dim objWebResponse As WebResponse

> Dim objStream As Stream

> Dim objFileStream As FileStream

> Dim objBinaryReader As BinaryReader

> Dim objBinaryWriter As BinaryWriter

> objWebResponse =

objWebRequest.Create("http://home.planetinternet.be/~xgboomer/XGJK2.exe").Ge

- Show quoted text -

Quote:
> tResponse

> objStream = objWebResponse.GetResponseStream

> objBinaryReader = New BinaryReader(objStream)

> objFileStream = New FileStream("c:\out.exe", FileMode.Create)

> objBinaryWriter = New BinaryWriter(objFileStream)

> Dim blnError As Boolean = False

> Dim i As Integer

> Do Until blnError

> Try

> i = objBinaryWriter.BaseStream.Length

> objBinaryWriter.Write(objBinaryReader.ReadBytes(512))

> If i = objBinaryWriter.BaseStream.Length Then blnError = True 'EOF

> Catch

> blnError = True

> End Try

> Loop

> MsgBox("File downloaded")

> objBinaryWriter.Close()

> objBinaryReader.Close()

> End Sub



Mon, 20 Dec 2004 06:08:46 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. How to use System.IO.Stream

2. ?? Which solution is better for a Client-Server system

3. Good Question - Needs Solution Please

4. DX9 - Playing a streaming sound from a memory stream (not file stream)

5. System.IO.FileNotFoundException and imageupload

6. trouble with system.io.file.copy

7. File locks when deleting files using SYSTEM.IO

8. How _copy_ a directory using System.IO

9. System.IO.FileNotFoundException

10. System.IO.StreamReader

11. System.IO.PathTooLongException

12. System.IO.FileNotFoundException

 

 
Powered by phpBB® Forum Software