Download Corrupts File!!!!!!!!

  • Thread starter Thread starter scorpion53061
  • Start date Start date
S

scorpion53061

For several reasons I needed to download an access database off the internet
in blocks instead of as a whole. However when using this code when I tried
to open the access database it created it reports that the database format
is unknown. Please help kind of desperate.....

..
local = File.Create("C:\newfile.mdb", 1023)
request = WebRequest.Create(filetoget)
response = request.GetResponse
stream = response.GetResponseStream()
Dim buffer(1023) As Byte
Try
With response.GetResponseStream
Do
n = stream.Read(buffer, 0, buffer.Length)
local.Write(buffer, 0, n)
Loop While n > 0
updatesuccessful = True
stream.Close()
local.Close()
MsgBox("complete")
End With
 
For several reasons I needed to download an access database off the internet
in blocks instead of as a whole. However when using this code when I tried
to open the access database it created it reports that the database format
is unknown. Please help kind of desperate.....

.
local = File.Create("C:\newfile.mdb", 1023)
request = WebRequest.Create(filetoget)
response = request.GetResponse
stream = response.GetResponseStream()
Dim buffer(1023) As Byte
Try
With response.GetResponseStream
Do
n = stream.Read(buffer, 0, buffer.Length)
local.Write(buffer, 0, n)
Loop While n > 0
updatesuccessful = True
stream.Close()
local.Close()
MsgBox("complete")
End With

Scorpion:

It looks a little funny.

1) I assume you have defined the variables as being of the proper datatypes.
2) I'd remove the 1023 from the File.Create line... don't think you'll need
it.
3) May as dim the buffer to 1024 I imagine.
4) You don't need the "With response.GetResponseStream" you're using
"stream"
5) You are testing the value of "n" a little late. It should matter but you
are writing "zero" bytes on the last read.
Consider a read, then in the loop write and read again until end of file
6) You have a Try that's it?

Otherwise it should work...

Test this one...

Dim sIn As String = "filetoget"
Dim sOut As String = "c:\newfile.mdb"

Dim request As System.Net.WebRequest =
System.Net.WebRequest.Create(sIn)
Dim response As System.Net.WebResponse = request.GetResponse
Dim fsIn As Stream = response.GetResponseStream

Dim length As Integer = 1024
Dim buffer(length) As Byte

Dim fsOut As FileStream = File.Create(sOut)

Dim nBytes As Integer = fsIn.Read(buffer, 0, length)

While nBytes > 0
fsOut.Write(buffer, 0, nBytes)
nBytes = fsIn.Read(buffer, 0, length)
End While

fsOut.Close()
fsIn.Close()
 
Tom,

Excellent Answer.

I am not sure what your code did that mine did not do. But I am very
grateful.
 
Back
Top