Public Function SaveRemoteFile(ByVal s_LocalFileName, ByVal s_RemoteFileUrl) Dim GetRemoteData
Dim bError, strHeader
bError = False
SaveRemoteFile = False
On Error Resume Next
Dim Retrieval
Set Retrieval = CreateObject("Msxml2.XMLHTTP")
With Retrieval
.Open "GET", s_RemoteFileUrl, False, "", ""
.send
If .ReadyState <> 4 Then Exit Function
strHeader = .getResponseHeader("Content-Type")
If Len(strHeader) = 0 Then Exit Function
GetRemoteData = .responseBody
End With
Set Retrieval = Nothing If LenB(GetRemoteData) < 100 Then Exit Function
Dim Ads
Set Ads = CreateObject("ADODB.Stream")
With Ads
.Type = 1
.Open
.Write GetRemoteData
.SaveToFile ChkMapPath(s_LocalFileName), 2
.Cancel
.Close
End With
Set Ads = Nothing
If Err.Number = 0 And bError = False Then
SaveRemoteFile = True
Else
SaveRemoteFile = False
Err.Clear
End If
End Function这段代码下载英文路径图片是没有问题的,但是一旦遇到带有中文的图片就下载不了。比如http://images1.fantong.com/attach/138/original/440x260_天下味.jpg
更奇怪的是,只要我在浏览器中打开过该远程图片或者打开含有该图片的网页,再用上面程序下载就可以把图片下载下来。
但是浏览器没有打开过该图片,就无法下载。
请问如何解决此问题?
Dim bError, strHeader
bError = False
SaveRemoteFile = False
On Error Resume Next
Dim Retrieval
Set Retrieval = CreateObject("Msxml2.XMLHTTP")
With Retrieval
.Open "GET", s_RemoteFileUrl, False, "", ""
.send
If .ReadyState <> 4 Then Exit Function
strHeader = .getResponseHeader("Content-Type")
If Len(strHeader) = 0 Then Exit Function
GetRemoteData = .responseBody
End With
Set Retrieval = Nothing If LenB(GetRemoteData) < 100 Then Exit Function
Dim Ads
Set Ads = CreateObject("ADODB.Stream")
With Ads
.Type = 1
.Open
.Write GetRemoteData
.SaveToFile ChkMapPath(s_LocalFileName), 2
.Cancel
.Close
End With
Set Ads = Nothing
If Err.Number = 0 And bError = False Then
SaveRemoteFile = True
Else
SaveRemoteFile = False
Err.Clear
End If
End Function这段代码下载英文路径图片是没有问题的,但是一旦遇到带有中文的图片就下载不了。比如http://images1.fantong.com/attach/138/original/440x260_天下味.jpg
更奇怪的是,只要我在浏览器中打开过该远程图片或者打开含有该图片的网页,再用上面程序下载就可以把图片下载下来。
但是浏览器没有打开过该图片,就无法下载。
请问如何解决此问题?
URL = URLEncodeUTF8(URL) 'utf-8格式Function URLEncodeUTF8(szInput As String) As String
Dim wch, uch, szRet
Dim x
Dim nAsc, nAsc2, nAsc3 If szInput = "" Then
Exit Function
End If For x = 1 To Len(szInput)
wch = Mid(szInput, x, 1)
nAsc = AscW(wch) If nAsc < 0 Then nAsc = nAsc + 65536 If (nAsc And &HFF80) = 0 Then
szRet = szRet & wch
Else
If (nAsc And &HF000) = 0 Then
uch = "%" & Hex(((nAsc \ 2 ^ 6)) Or &HC0) & "%" & Hex(nAsc And &H3F Or &H80)
szRet = szRet & uch
Else
uch = "%" & Hex((nAsc \ 2 ^ 12) Or &HE0) & "%" & _
Hex((nAsc \ 2 ^ 6) And &H3F Or &H80) & "%" & _
Hex(nAsc And &H3F Or &H80)
szRet = szRet & uch
End If
End If
Next szRet = Replace$(szRet, " ", "%20") URLEncodeUTF8 = szRet
End Function Function URLEncoding(vstrIn) As String
strReturn = ""
Dim i
For i = 1 To Len(vstrIn)
ThisChr = Mid(vstrIn, i, 1)
If Abs(Asc(ThisChr)) < &HFF Then
strReturn = strReturn & ThisChr
Else
innerCode = Asc(ThisChr)
If innerCode < 0 Then
innerCode = innerCode + &H10000
End If
Hight8 = (innerCode And &HFF00) \ &HFF
Low8 = innerCode And &HFF
strReturn = strReturn & "%" & Hex(Hight8) & "%" & Hex(Low8)
End If
Next
strReturn = Replace(strReturn, Chr(32), "%20")
URLEncoding = strReturn
End Function