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

时间:2018-10-25 01:32:29

标签: vbscript

下面的代码,我在不使用循环功能的情况下上载了没有循环的代码,并且代码工作正常。

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

但是当我在循环中使用上述代码时,在第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

1 个答案:

答案 0 :(得分:1)

您无法在Function内定义Loop

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

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