Microsoft KB Archive/200676

= How To Use the AsyncRead Method to Download Files =

Article ID: 200676

Article Last Modified on 8/10/2004

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Professional Edition

-



This article was previously published under Q200676



SUMMARY
Both UserDocument objects and UserControl objects can perform asynchronous file reading from local and remote computers. This article demonstrates how to use the AsyncRead method of a UserControl to copy a file.



Step-by-Step Example
The following steps demonstrate how to copy a file from an Internet site.  Create an ActiveX control project in Visual Basic. UserControl1 is created by default. Add two Label controls, a TextBox control, and a CommandButton control to the UserControl designer. Position Label1 above Text1, and adjust the size of the TextBox control to handle approximately 100 characters.  Add the following code to the General Declarations section of UserControl1: Option Explicit

Private Sub Command1_Click ' Use the AsyncRead method to copy the file. UserControl.AsyncRead Text1.Text, vbAsyncTypeFile End Sub

Private Sub UserControl_AsyncReadComplete(AsyncProp As AsyncProperty) ' Yield execution to ensure that the temporary file is written. DoEvents ' Display the copied file name, including its path, ' using the value property of the AsyncProp object. MsgBox Text1.Text & " was copied to: " & AsyncProp.Value End Sub

Private Sub UserControl_AsyncReadProgress(AsyncProp As AsyncProperty) ' Display the progress of the file copy using the ' BytesRead and BytesMax properties of the AsyncProp object. Label2.Caption = AsyncProp.BytesRead & " of " & AsyncProp.BytesMax & " bytes copied." End Sub

Private Sub UserControl_Initialize Command1.Caption = "Copy!" Label1.Caption = "From:" Label2.Caption = "" ' Assign a file to be copied. ' Use the Visual Basic 6.0 run-time files package as a test. Text1.Text = "http://download.microsoft.com/download/vb60pro/install/6/Win98Me/EN-US/VBRun60.exe" End Sub  Run the project., In the Project Properties dialog box on the Debugging tab, click OK to start the UserControl1 component. Notice that UserControl1 is now running in a Web browser. Locate a file that you want to copy from a remote computer through HTTP, and type the URL and file name in the TextBox. Click Copy!. A dialog box appears when the copy operation is complete.</li></ol>

<div class="references_section">