function DownloadFile(url as String, fileName as String) as object
returnValue = {}
urlObj = CreateObject("roUrlTransfer")
urlObj.SetRequest("GET")
urlObj.SetUrl(url.EncodeUri())
urlObj.RetainBodyOnError(true)
urlObj.SetCertificatesFile("common:/certs/ca-bundle.crt")
urlObj.InitClientCertificates()
urlObj.AddHeader("X-Roku-Reserved-Dev-Id", "")
urlObj.AddHeader("Content-Type", "text/plain")
urlObj.AddHeader("Accept", "*/*")
port = CreateObject("roMessagePort")
urlObj.SetMessagePort(port)
filePath = "cachefs:/" + fileName
if(urlObj.AsyncGetToFile(filePath))
while(true)
msg = wait(0, port)
if(type(msg) = "roUrlEvent")
code = msg.GetResponseCode()
if(code = 200)
returnValue.success = true
returnValue.filePath = filePath
else
returnValue.success = false
returnValue.code = code
returnValue.errorMessage = "Failed to download file: '" + fileName + "'. Code: " + code.toStr() + ". Reason: " + msg.GetFailureReason()
urlObj.AsyncCancel()
end if
else
returnValue.success = false
returnValue.errorMessage = "Invalid event"
urlObj.AsyncCancel()
end if
exit while
end while
else
returnValue.success = false
returnValue.errorMessage = "Download request could not be issued."
urlObj.AsyncCancel()
end if
return returnValue
end function