我一直在成功使用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