Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bytes and Writing File Problems

Status
Not open for further replies.

PCSAARON

Programmer
Jul 9, 2002
131
US
I am downloading files with an HTTP Post. The cgi-script sends the files when requested. This little peice of code saves the files as they are sent. The only problem is that when the Put# is used with the variant on the FIRST call and when it is called the second time within the loop, it will PAD 12 bytes of data to the "chunk" when it saves it. I think it has to do with the vtData variant variable being a variant.

These 12 bytes looks like: 1120 0100 D002 0000 0000 0000

I don't want to have to strip these from the file everytime. That would be nonsense...

Does anyone have any different methods of writing chunks of binary files? Thanks in advance...

Aaron


Dim vtData As Variant ' Data variable.

Dim nFileNum As Integer

nFileNum = FreeFile

Kill App.Path & "\" & FileServerList(FileBuffer, 1) 'delete any existing file
Open App.Path & "\" & FileServerList(FileBuffer, 1) For Binary Access Write Lock Read Write As #nFileNum

' Get the first chunk. NOTE: specify a Byte
vtData = Inet2.GetChunk(1024, icByteArray)

Do While LenB(vtData) > 0
Put #nFileNum, , vtData
ProgressBar1.value = (Int((Len(vtData) / CLng(FileServerList(FileBuffer, 6))) * 100)) * 2
lblProgressInfo.Caption = "Current File: " & FileServerList(FileBuffer, 1) & " - Downloaded " & CStr(Len(strText)) & " bytes (" & ProgressBar1.value & "%)"
' Get next chunk.
vtData = Inet2.GetChunk(1024, icByteArray)
Loop
Put #nFileNum, , vtData
Close #nFileNum
 
I found the answer on my own. It definitely was the variant type that was giving bogus results. I used a redim on another variable throughout the chunker to get the exact size of the variant that was chunked. Here is the fixed code:

Dim vtData As Variant ' Data variable.
Dim tempData() As Byte

Dim nFileNum As Integer

nFileNum = FreeFile
'--------------------
ProgressBar1.value = 0.001
Kill App.Path & "\" & FileServerList(FileBuffer, 1) 'delete any existing file
Resume_Not_Found:
Open App.Path & "\" & FileServerList(FileBuffer, 1) For Binary Access Write As #nFileNum


vtData = Inet2.GetChunk(1024, icByteArray) ' Get the first chunk
ReDim tempData(LenB(vtData))
tempData = vtData
Do While LenB(vtData) > 0
Put #nFileNum, , tempData
ProgressBar1.value = (Int((Len(vtData) / CLng(FileServerList(FileBuffer, 6))) * 100)) * 2
lblProgressInfo.Caption = "Current File: " & FileServerList(FileBuffer, 1) & " - Downloaded " & CStr(Len(strText)) & " bytes (" & ProgressBar1.value & "%)"
vtData = Inet2.GetChunk(1024, icByteArray) ' Get next chunk.
ReDim tempData(LenB(vtData))
tempData = vtData
Loop
If LenB(vtData) > 0 Then
Put #nFileNum, , vtData
End If

Close #nFileNum


If anyone has a better way than this, please post. Thanks.

Aaron
 
One problem with this download method; when the file downlods, the processes SLOW down to a crawl when the buffer starts to fill up. Does anyone have any suggestions? Since INET is asynchronous, it will download the file in the background. I think it is filling the buffer and then slowing things down, especially if it is a 1 or 2 meg download.

Aaron
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top