我一直在成功使用URLDownloadToFile功能一年,但最近它只是停止工作(或与网络的东西)。URLDownloadToFile据说工作,但文件损坏或不正确
的代码中的相关行: success = URLDownloadToFile(0, URL, Target, 0, 0)
在试图打开它,我得到以下信息:
Excel无法打开文件“wkbk.xlsm”,因为文件格式或文件扩展名无效。验证文件是否已损坏,文件扩展名是否与文件的格式相匹配。
当我在我的文本编辑器打开该文件,我看到它确实是HTML: 我已经star'd了域和路径window.location...
出于安全原因
<script src="/edms/scripts/JRETest.js"></script>
<script>
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toGMTString();
document.cookie = cname + "=" + cvalue + "; " + expires + ";domain=***;path=/";
}
if (JRETest.versionCheck('1.8+')) {
setCookie("JRECookie", "1.7", 5);
} else if (JRETest.versionCheck('1.7+')) {
setCookie("JRECookie", "1.7", 5);
} else if (JRETest.versionCheck('1.6+')) {
setCookie("JRECookie", "1.6", 5);
} else {
setCookie("JRECookie", "1.6", 5);
}
window.location = "<link that I passed to URLDownloadToFile>";
</script>
</html>
但是,当我将文件的URL放入浏览器时,它会自动下载它,就像它总是这样,所以URL是正确的,如果我运行这个GetURLStatus
函数,它将返回200 - OK
Function GetURLStatus(ByVal URL As String, Optional AllowRedirects As Boolean)
Const WinHttpRequestOption_UserAgentString = 0
Const WinHttpRequestOption_EnableRedirects = 6
On Error Resume Next
Set httpRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
If httpRequest Is Nothing Then
Set httpRequest = CreateObject("WinHttp.WinHttpRequest.5")
End If
Err.Clear
On Error GoTo 0
httpRequest.Option(WinHttpRequestOption_UserAgentString) = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)"
httpRequest.Option(WinHttpRequestOption_EnableRedirects) = AllowRedirects
'Clear any pervious web page source information
PageSource = ""
'Add protocol if missing
If InStr(1, URL, "://") = 0 Then
URL = "http://" & URL
End If
'Launch the HTTP httpRequest synchronously
On Error Resume Next
httpRequest.Open "GET", URL, False
If Err.Number <> 0 Then
'Handle connection errors
GetURLStatus = Err.Description
Err.Clear
Exit Function
End If
On Error GoTo 0
'Send the http httpRequest for server status
On Error Resume Next
httpRequest.Send
httpRequest.WaitForResponse
If Err.Number <> 0 Then
' Handle server errors
PageSource = "Error"
GetURLStatus = Err.Description
Err.Clear
Else
'Show HTTP response info
GetURLStatus = httpRequest.Status & " - " & httpRequest.StatusText
'Save the web page text
PageSource = httpRequest.responsetext
End If
On Error GoTo 0
End Function
就像我说的,这一切都工作过,所以我不知道如果内部脚本改变,如果它与我们的内部文件系统的可能,如果有任何其他的方式我可以叫URLDownloadToFile要么等待一会儿,直到位置改变,或者真正发生了什么。
我会说你的一个管理员开始使用你的公司代理玩你试图打开F12并开始网络跟踪,而你输入链接到浏览器?我相信你会看到一些浏览器可以复制的重定向,但不是你的下载脚本。 – Marged
辉煌!这一定是发生了什么,因为使用其中一个重定向请求的URL我能够得到正确的文件。如果你想让这个答案,我会标记它是正确的。 – redOctober13