每 10 秒在 Web 服务器中上传文件时出错

Error while uploading file in web server in every 10 sec

以下代码我用于不使用循环上传代码,当我不使用循环功能时代码工作正常。

dim strURL
Dim password
Dim username 
Set wshShell = CreateObject("WScript.Shell")
UploadDest= wshShell.ExpandEnvironmentStrings("https://webdav.pcloud.com/Public%20Folder/")
UploadFile = wshShell.ExpandEnvironmentStrings("%username%.txt")
 username = "abc@gmail.com"
 password = "abc"
sData = getFileBytes(UploadFile, UploadType)
Set HttpReq =createobject("Microsoft.XMLHTTP")
strURL = UploadDest & "/" & UploadFile
HttpReq.Open "PUT",strURL,False,username,password
HttpReq.send sData 
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

但是当我在循环中使用上面的代码时,我在 no:12

行出现语法错误
function getFileBytes(flnm,sType)

我使用以下代码每 10 秒循环上传文件。

set i = 0
Do While i = 0
dim strURL
Dim password
Dim username 
Set wshShell = CreateObject("WScript.Shell")
UploadDest= wshShell.ExpandEnvironmentStrings("https://webdav.pcloud.com/Public%20Folder/")
UploadFile = wshShell.ExpandEnvironmentStrings("%username%.txt")
 username = "abc@gmail.com"
 password = "abc"
sData = getFileBytes(UploadFile, UploadType)
Set HttpReq =createobject("Microsoft.XMLHTTP")
strURL = UploadDest & "/" & UploadFile
HttpReq.Open "PUT",strURL,False,username,password
HttpReq.send sData 
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
WScript.Sleep(10000)
loop

您不能在 Loop

中定义 Function

像这样重写以在循环外定义函数:

Dim i
Do While i = 0
    dim strURL
    Dim password
    Dim username 
    Set wshShell = CreateObject("WScript.Shell")
    UploadDest= wshShell.ExpandEnvironmentStrings("https://webdav.pcloud.com/Public%20Folder/")
    UploadFile = wshShell.ExpandEnvironmentStrings("%username%.txt")
    username = "abc@gmail.com"
    password = "abc"
    sData = getFileBytes(UploadFile, UploadType)
    Set HttpReq =createobject("Microsoft.XMLHTTP")
    strURL = UploadDest & "/" & UploadFile
    HttpReq.Open "PUT",strURL,False,username,password
    HttpReq.send sData 
    WScript.Sleep(10000)
loop

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