Introduction
The example VBS script below shows how to upload and save a local file to a remote webserver using Microsoft VBS scripting. The upload method in this example is HTTP PUT, meaning that the webserver needs to be WebDAV-enabled. Since file upload is typically password-protected, the script implements access with basic authentication.
'======================================================================
' https-upload.vbs 1.0 @2009 by Frank4dd http://fm4dd.com/programming
' This script demonstrates a file upload to a WebDAV enabled webserver,
' using https (and proxy settings from IE) with basic web authentication
'
' Original authors and code references:
' - "ASP - File upload with HTTP Put" by Martin Clark
'
' This program comes with ABSOLUTELY NO WARRANTY. You may redistribute
' copies of it under the terms of the GNU General Public License.
'======================================================================
'======================================================================
' Global Constants and Variables
'======================================================================
Const scriptVer = "1.0"
Const UploadDest = "https://mywebdavserver.com/uploadurl"
Const UploadFile = "localpath-and-file"
Const UploadUser = "username"
Const UploadPass = "password"
Const UploadType = "binary"
dim strURL
function sendit()
sData = getFileBytes(UploadFile, UploadType)
sfileName= mid(UploadFile, InstrRev(UploadFile,"\")+1,len(UploadFile))
dim xmlhttp
set xmlhttp=createobject("MSXML2.XMLHTTP.3.0")
strURL = UploadDest & "/" & UploadFile
msgbox "Upload-URL: " & strURL
xmlhttp.Open "PUT", strURL, false, UploadUser, UploadPass
xmlhttp.Send sData
Wscript.Echo "Upload-Status: " & xmlhttp.statusText
set xmlhttp=Nothing
End function
function showresult()
Wscript.Echo "Complete. Check upload success at: " & strURL
end function
function getFileBytes(flnm, sType)
Dim objStream
Set objStream = CreateObject("ADODB.Stream")
if sType="binary" then
objStream.Type = 1 ' adTypeBinary
else
objStream.Type = 2 ' adTypeText
objStream.Charset ="ascii"
end if
objStream.Open
objStream.LoadFromFile flnm
if sType="binary" then
getFileBytes=objStream.Read 'read binary'
else
getFileBytes= objStream.ReadText 'read ascii'
end if
objStream.Close
Set objStream = Nothing
end function
'=======================================================================
' End Function Defs, Start Main
'=======================================================================
' Get cmdline params and initialize variables
If Wscript.Arguments.Named.Exists("h") Then
Wscript.Echo "Usage: https-upload.vbs"
Wscript.Echo "version " & scriptVer
WScript.Quit(intOK)
End If
sendit()
showresult()
Wscript.Quit(intOK)
'=======================================================================
' End Main
'=======================================================================