vb upload subprocedure? 
Author Message
 vb upload subprocedure?
hi everybodi i'm a little new to vb programmin esp on the internet
part....seems microsoft likes to make things odd.
aniwae, i tried to churn out a sub that can download files via http and ftp
and now just wondering if we can do something like that for upload too.
okae, here's the sub, u need wininet.dll for it to work.

<!-- start copying from here. paste into declaration section-->

' Download a file from Internet and save it to a local file
'
' it works with HTTP and FTP, but you must explicitly include
' the protocol name in the URL, as in
' Download( " http://www.*-*-*.com/ ;, "C:\vb.htm")

Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Const INTERNET_FLAG_EXISTING_CONNECT = &H20000000

Private Declare Function InternetOpen Lib "wininet.dll" Alias
"InternetOpenA" (ByVal lpszAgent As String, ByVal dwAccessType As Long,
ByVal lpszProxyName As String, ByVal lpszProxyBypass As String, ByVal
dwFlags As Long) As Long
Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias
"InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal lpszUrl As String,
ByVal lpszHeaders As String, ByVal dwHeadersLength As Long, ByVal dwFlags As
Long, ByVal dwContext As Long) As Long
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet
As Long) As Integer
Private Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As
Long, ByVal lpBuffer As String, ByVal dwNumberOfBytesToRead As Long,
lNumberOfBytesRead As Long) As Integer

Sub Download(ByVal Url As String, ByVal FileName As String)
    Dim hInternetSession As Long
    Dim hUrl As Long
    Dim FileNum As Integer
    Dim OK As Boolean
    Dim NumberOfBytesRead As Long
    Dim Buffer As String
    Dim fileIsOpen As Boolean

    On Error GoTo ErrorHandler

    ' check obvious syntax errors
    If Len(URL) = 0 Or Len(FileName) = 0 Then Err.Raise 5

    ' open an Internet session, and retrieve its handle
    hInternetSession = InternetOpen(App.EXEName,
INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
    If hInternetSession = 0 Then Err.Raise vbObjectError + 1000, , "An error
occurred calling InternetOpen function"

    ' open the file and retrieve its handle
    hUrl = InternetOpenUrl(hInternetSession, URL, vbNullString, 0,
INTERNET_FLAG_EXISTING_CONNECT, 0)
    If hUrl = 0 Then Err.Raise vbObjectError + 1000, , "An error occurred
calling InternetOpenUrl function"

    ' ensure that there is no local file
    On Error Resume Next
    Kill FileName

    On Error GoTo ErrorHandler

    ' open the local file
    FileNum = FreeFile
    Open FileName For Binary As FileNum
    fileIsOpen = True

    ' prepare the receiving buffer
    Buffer = Space(4096)

    Do
        ' read a chunk of the file - returns True if no error
        ok = InternetReadFile(hUrl, Buffer, Len(Buffer), NumberOfBytesRead)

        ' exit if error or no more data
        If NumberOfBytesRead = 0 Or Not ok Then Exit Do

        ' save the data to the local file
        Put #FileNum, , Left$(Buffer, NumberOfBytesRead)
    Loop

    ' flow into the error handler

ErrorHandler:
    ' close the local file, if necessary
    If fileIsOpen Then Close #FileNum
    ' close internet handles, if necessary
    If hUrl Then InternetCloseHandle hUrl
    If hInternetSession Then InternetCloseHandle hInternetSession

    ' report the error to the client, if there is one
    If Err Then Err.Raise Err.Number, , Err.Description
End Sub

<!-- end copying here -->

ps. anibody can figure out how to do call
upload("c:\vbsux.ext"," http://www.*-*-*.com/ ;)?

--

     >
     -



Fri, 23 May 2003 03:00:00 GMT  
 vb upload subprocedure?
Take a look at http://kpdteam.tripod.com/api/api299.htm

Regards,
Pieter Philippaerts
http://www.allapi.net/



Quote:
> hi everybodi i'm a little new to vb programmin esp on the internet
> part....seems microsoft likes to make things odd.
> aniwae, i tried to churn out a sub that can download files via http and
ftp
> and now just wondering if we can do something like that for upload too.
> okae, here's the sub, u need wininet.dll for it to work.

> <!-- start copying from here. paste into declaration section-->

> ' Download a file from Internet and save it to a local file
> '
> ' it works with HTTP and FTP, but you must explicitly include
> ' the protocol name in the URL, as in
> ' Download( "http://www.vb.com/newvbfile.asp", "C:\vb.htm")

> Const INTERNET_OPEN_TYPE_PRECONFIG = 0
> Const INTERNET_FLAG_EXISTING_CONNECT = &H20000000

> Private Declare Function InternetOpen Lib "wininet.dll" Alias
> "InternetOpenA" (ByVal lpszAgent As String, ByVal dwAccessType As Long,
> ByVal lpszProxyName As String, ByVal lpszProxyBypass As String, ByVal
> dwFlags As Long) As Long
> Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias
> "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal lpszUrl As
String,
> ByVal lpszHeaders As String, ByVal dwHeadersLength As Long, ByVal dwFlags
As
> Long, ByVal dwContext As Long) As Long
> Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal
hInet
> As Long) As Integer
> Private Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile
As
> Long, ByVal lpBuffer As String, ByVal dwNumberOfBytesToRead As Long,
> lNumberOfBytesRead As Long) As Integer

> Sub Download(ByVal Url As String, ByVal FileName As String)
>     Dim hInternetSession As Long
>     Dim hUrl As Long
>     Dim FileNum As Integer
>     Dim OK As Boolean
>     Dim NumberOfBytesRead As Long
>     Dim Buffer As String
>     Dim fileIsOpen As Boolean

>     On Error GoTo ErrorHandler

>     ' check obvious syntax errors
>     If Len(URL) = 0 Or Len(FileName) = 0 Then Err.Raise 5

>     ' open an Internet session, and retrieve its handle
>     hInternetSession = InternetOpen(App.EXEName,
> INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
>     If hInternetSession = 0 Then Err.Raise vbObjectError + 1000, , "An
error
> occurred calling InternetOpen function"

>     ' open the file and retrieve its handle
>     hUrl = InternetOpenUrl(hInternetSession, URL, vbNullString, 0,
> INTERNET_FLAG_EXISTING_CONNECT, 0)
>     If hUrl = 0 Then Err.Raise vbObjectError + 1000, , "An error occurred
> calling InternetOpenUrl function"

>     ' ensure that there is no local file
>     On Error Resume Next
>     Kill FileName

>     On Error GoTo ErrorHandler

>     ' open the local file
>     FileNum = FreeFile
>     Open FileName For Binary As FileNum
>     fileIsOpen = True

>     ' prepare the receiving buffer
>     Buffer = Space(4096)

>     Do
>         ' read a chunk of the file - returns True if no error
>         ok = InternetReadFile(hUrl, Buffer, Len(Buffer),
NumberOfBytesRead)

>         ' exit if error or no more data
>         If NumberOfBytesRead = 0 Or Not ok Then Exit Do

>         ' save the data to the local file
>         Put #FileNum, , Left$(Buffer, NumberOfBytesRead)
>     Loop

>     ' flow into the error handler

> ErrorHandler:
>     ' close the local file, if necessary
>     If fileIsOpen Then Close #FileNum
>     ' close internet handles, if necessary
>     If hUrl Then InternetCloseHandle hUrl
>     If hInternetSession Then InternetCloseHandle hInternetSession

>     ' report the error to the client, if there is one
>     If Err Then Err.Raise Err.Number, , Err.Description
> End Sub

> <!-- end copying here -->

> ps. anibody can figure out how to do call
> upload("c:\vbsux.ext","http://www.microsoft.com/vbcomment/vbdonsux.ext")?

> --
> / /| |\\       w/o the keybd, Gates is nothing

>      -



Fri, 23 May 2003 03:00:00 GMT  
 vb upload subprocedure?
444, I know you were looking for code to upload a file, but I have been in
need of a way to upload without any added controls, the code you uploaded
was perfect.  Thanks for posting it.

Martin Fletcher


Quote:
> hi everybodi i'm a little new to vb programmin esp on the internet
> part....seems microsoft likes to make things odd.
> aniwae, i tried to churn out a sub that can download files via http and
ftp
> and now just wondering if we can do something like that for upload too.
> okae, here's the sub, u need wininet.dll for it to work.

> <!-- start copying from here. paste into declaration section-->

> ' Download a file from Internet and save it to a local file
> '
> ' it works with HTTP and FTP, but you must explicitly include
> ' the protocol name in the URL, as in
> ' Download( "http://www.vb.com/newvbfile.asp", "C:\vb.htm")

> Const INTERNET_OPEN_TYPE_PRECONFIG = 0
> Const INTERNET_FLAG_EXISTING_CONNECT = &H20000000

> Private Declare Function InternetOpen Lib "wininet.dll" Alias
> "InternetOpenA" (ByVal lpszAgent As String, ByVal dwAccessType As Long,
> ByVal lpszProxyName As String, ByVal lpszProxyBypass As String, ByVal
> dwFlags As Long) As Long
> Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias
> "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal lpszUrl As
String,
> ByVal lpszHeaders As String, ByVal dwHeadersLength As Long, ByVal dwFlags
As
> Long, ByVal dwContext As Long) As Long
> Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal
hInet
> As Long) As Integer
> Private Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile
As
> Long, ByVal lpBuffer As String, ByVal dwNumberOfBytesToRead As Long,
> lNumberOfBytesRead As Long) As Integer

> Sub Download(ByVal Url As String, ByVal FileName As String)
>     Dim hInternetSession As Long
>     Dim hUrl As Long
>     Dim FileNum As Integer
>     Dim OK As Boolean
>     Dim NumberOfBytesRead As Long
>     Dim Buffer As String
>     Dim fileIsOpen As Boolean

>     On Error GoTo ErrorHandler

>     ' check obvious syntax errors
>     If Len(URL) = 0 Or Len(FileName) = 0 Then Err.Raise 5

>     ' open an Internet session, and retrieve its handle
>     hInternetSession = InternetOpen(App.EXEName,
> INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
>     If hInternetSession = 0 Then Err.Raise vbObjectError + 1000, , "An
error
> occurred calling InternetOpen function"

>     ' open the file and retrieve its handle
>     hUrl = InternetOpenUrl(hInternetSession, URL, vbNullString, 0,
> INTERNET_FLAG_EXISTING_CONNECT, 0)
>     If hUrl = 0 Then Err.Raise vbObjectError + 1000, , "An error occurred
> calling InternetOpenUrl function"

>     ' ensure that there is no local file
>     On Error Resume Next
>     Kill FileName

>     On Error GoTo ErrorHandler

>     ' open the local file
>     FileNum = FreeFile
>     Open FileName For Binary As FileNum
>     fileIsOpen = True

>     ' prepare the receiving buffer
>     Buffer = Space(4096)

>     Do
>         ' read a chunk of the file - returns True if no error
>         ok = InternetReadFile(hUrl, Buffer, Len(Buffer),
NumberOfBytesRead)

>         ' exit if error or no more data
>         If NumberOfBytesRead = 0 Or Not ok Then Exit Do

>         ' save the data to the local file
>         Put #FileNum, , Left$(Buffer, NumberOfBytesRead)
>     Loop

>     ' flow into the error handler

> ErrorHandler:
>     ' close the local file, if necessary
>     If fileIsOpen Then Close #FileNum
>     ' close internet handles, if necessary
>     If hUrl Then InternetCloseHandle hUrl
>     If hInternetSession Then InternetCloseHandle hInternetSession

>     ' report the error to the client, if there is one
>     If Err Then Err.Raise Err.Number, , Err.Description
> End Sub

> <!-- end copying here -->

> ps. anibody can figure out how to do call
> upload("c:\vbsux.ext","http://www.microsoft.com/vbcomment/vbdonsux.ext")?

> --
> / /| |\\       w/o the keybd, Gates is nothing

>      -



Fri, 23 May 2003 03:00:00 GMT  
 vb upload subprocedure?
If you do happen to find a way to upload to an ftp server can you please
post the code? or e-mail it to me?

Martin Fletcher


Quote:
> hi everybodi i'm a little new to vb programmin esp on the internet
> part....seems microsoft likes to make things odd.
> aniwae, i tried to churn out a sub that can download files via http and
ftp
> and now just wondering if we can do something like that for upload too.
> okae, here's the sub, u need wininet.dll for it to work.

> <!-- start copying from here. paste into declaration section-->

> ' Download a file from Internet and save it to a local file
> '
> ' it works with HTTP and FTP, but you must explicitly include
> ' the protocol name in the URL, as in
> ' Download( "http://www.vb.com/newvbfile.asp", "C:\vb.htm")

> Const INTERNET_OPEN_TYPE_PRECONFIG = 0
> Const INTERNET_FLAG_EXISTING_CONNECT = &H20000000

> Private Declare Function InternetOpen Lib "wininet.dll" Alias
> "InternetOpenA" (ByVal lpszAgent As String, ByVal dwAccessType As Long,
> ByVal lpszProxyName As String, ByVal lpszProxyBypass As String, ByVal
> dwFlags As Long) As Long
> Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias
> "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal lpszUrl As
String,
> ByVal lpszHeaders As String, ByVal dwHeadersLength As Long, ByVal dwFlags
As
> Long, ByVal dwContext As Long) As Long
> Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal
hInet
> As Long) As Integer
> Private Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile
As
> Long, ByVal lpBuffer As String, ByVal dwNumberOfBytesToRead As Long,
> lNumberOfBytesRead As Long) As Integer

> Sub Download(ByVal Url As String, ByVal FileName As String)
>     Dim hInternetSession As Long
>     Dim hUrl As Long
>     Dim FileNum As Integer
>     Dim OK As Boolean
>     Dim NumberOfBytesRead As Long
>     Dim Buffer As String
>     Dim fileIsOpen As Boolean

>     On Error GoTo ErrorHandler

>     ' check obvious syntax errors
>     If Len(URL) = 0 Or Len(FileName) = 0 Then Err.Raise 5

>     ' open an Internet session, and retrieve its handle
>     hInternetSession = InternetOpen(App.EXEName,
> INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
>     If hInternetSession = 0 Then Err.Raise vbObjectError + 1000, , "An
error
> occurred calling InternetOpen function"

>     ' open the file and retrieve its handle
>     hUrl = InternetOpenUrl(hInternetSession, URL, vbNullString, 0,
> INTERNET_FLAG_EXISTING_CONNECT, 0)
>     If hUrl = 0 Then Err.Raise vbObjectError + 1000, , "An error occurred
> calling InternetOpenUrl function"

>     ' ensure that there is no local file
>     On Error Resume Next
>     Kill FileName

>     On Error GoTo ErrorHandler

>     ' open the local file
>     FileNum = FreeFile
>     Open FileName For Binary As FileNum
>     fileIsOpen = True

>     ' prepare the receiving buffer
>     Buffer = Space(4096)

>     Do
>         ' read a chunk of the file - returns True if no error
>         ok = InternetReadFile(hUrl, Buffer, Len(Buffer),
NumberOfBytesRead)

>         ' exit if error or no more data
>         If NumberOfBytesRead = 0 Or Not ok Then Exit Do

>         ' save the data to the local file
>         Put #FileNum, , Left$(Buffer, NumberOfBytesRead)
>     Loop

>     ' flow into the error handler

> ErrorHandler:
>     ' close the local file, if necessary
>     If fileIsOpen Then Close #FileNum
>     ' close internet handles, if necessary
>     If hUrl Then InternetCloseHandle hUrl
>     If hInternetSession Then InternetCloseHandle hInternetSession

>     ' report the error to the client, if there is one
>     If Err Then Err.Raise Err.Number, , Err.Description
> End Sub

> <!-- end copying here -->

> ps. anibody can figure out how to do call
> upload("c:\vbsux.ext","http://www.microsoft.com/vbcomment/vbdonsux.ext")?

> --
> / /| |\\       w/o the keybd, Gates is nothing

>      -



Fri, 23 May 2003 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. vb upload subprocedure?

2. "On Error" - displaying subprocedure name

3. can't run subprocedure

4. Replacing a Word Command with My Own Subprocedure from VB6 (Not VBA)

5. Help on Basic Subprocedure to making a password

6. Erasing text boxes subprocedure

7. optional parameters in a subprocedure

8. Newbie: Syntax Error on Subprocedure

9. Replacing a Word Command with My Own Subprocedure from VB6 (Not VBA)

10. Datei Upload per HTTP / File upload via HTTP

11. Upload / Download ( Transfer ) files with VB.NET ?

12. File upload from a vb.net client to a webserver

 

 
Powered by phpBB® Forum Software