【问题标题】:Download a file (zip or jpg) from a webpage从网页下载文件(zip 或 jpg)
【发布时间】:2016-12-07 17:31:20
【问题描述】:

我在从网页下载图像(有时是 zip)时遇到了一些问题。 我已经查看了一些关于该主题的论坛,大部分时间他们建议使用 URLDownloadToFile 函数。 我尝试应用它,但它似乎不起作用。

这是我正在处理的网页类型的示例:

这里的类型是 jpg,但有时也可以是 zip。 对于jpg的情况,我有两种方法:

  1. 单击查看按钮,这将打开一个包含仅1张图片的新网页,选择该网页并以某种方式下载图片,但我无法做到做。
    (当您手动单击图片时会出现“将图片另存为”,但是如何使用 VBA 访问它?):

                  objIE.document.frames(1).frames(1).document.getElementById("notPrintable").document.getElementsByName("view")(0).Click 'This clicks on the View Button
                  attachment_url = "https://pumapgf-row.bmwgroup.net/puma/case/showfile.do?selectedIndex=" & elem_id & "&filename=" & elem_name & "%20%7C%20jpg%20%7C%20" & end_url ' this is the url of the new webpage which is opened when I click the view button
    
                  Set objIE = IEWindowFromLocation(attachment_url) ' I select the new webpage
                  Set IEDoc = objIE.document ' set document on it
    

    这个新网页的 html 当然是 jpg)看起来像这样: 我当时尝试做但未成功的是以这种方式使用 URLDownloadToFile 函数

     Dim myImages As IHTMLElementCollection
     Set myImages = IEDoc.getElementsByTagName("img")
     returnValue = URLDownloadToFile(0, myImages(0).href, "P:\Alex\ABC.img", 0, 0)
    

    无论我在运行代码之前是否创建了这样的调用文件,都没有任何区别。我也尝试过使用 .jpg、.img、.png。 myImages(0).href 像这样结束:

    所以我不知道 .href 不以 .jpg 或 .img 之类结尾的事实是否存在问题。

  2. 单击 另存为 按钮:对 jpg 和 zip 文件均有效,因此会是更好的解决方案。我当然设法点击它,但问题在于互联网显示此,我不知道如何处理它。

知道怎么做吗?

编辑:Here is the properties window of the image

【问题讨论】:

  • 只需下载它并检查文件是否以jpg标题(0xFFD8FFE0)或(0xFFD8FF01)开头。
  • 不确定是否理解您的评论。您可以在我帖子的最后一张图片上看到该图片的名称(以 UX69525 开头)
  • 我不是在谈论文件名,我是在谈论您下载的实际二进制文件。
  • 我如何/在哪里可以实际检查这个?

标签: html vba download


【解决方案1】:

假设您有一个有效的下载 URL(我无法根据您问题中的站点对其进行测试),您需要做的就是测试文件是否为 jpg 文件并下载它并检查是否存在JPEG file header:

Public Function FileIsJpg(filepath As String) As Boolean
    Dim handle As Long
    handle = FreeFile
    Open filepath For Binary As #handle
    Dim header As Integer
    Get #handle, , header
    'Note the byte order.
    If header = &HD8FF Then
        Get #handle, , header
        If header = &HE0FF Or header = &H1FF Then
            FileIsJpg = True
        End If
    End If
    Close #handle
End Function

请注意,对于您的使用,这将需要错误处理,因为URLDownloadToFile 仍有可能打开文件。我假设你有某种等待机制(它是一个非阻塞函数)。如果没有,您需要使用本机回调机制或猜测并使用Application.Wait 或类似的东西。

使用示例:

Private Declare Function URLDownloadToFile Lib "urlmon" _
    Alias "URLDownloadToFileA" (ByVal pCaller As Long, _
    ByVal szURL As String, ByVal szFileName As String, _
    ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

Private Const S_OK As Long = 0

Sub Examples()
    Const TestJpgUrl As String = "https://www.gstatic.com/webp/gallery/1.jpg"
    Const TestPngUrl As String = "https://www.gstatic.com/webp/gallery3/1.png"

    Dim target As String
    target = Environ$("TEMP") & "\test.png"
    If URLDownloadToFile(0, TestPngUrl, target, 0, 0) = S_OK Then
        'Wait for download to complete - a callback function would be better.
        Application.Wait Now + TimeSerial(0, 0, 1)
        MsgBox target & ": " & FileIsJpg(target)
    End If
    Kill target

    target = Environ$("TEMP") & "\test.jpg"
    If URLDownloadToFile(0, TestJpgUrl, target, 0, 0) = S_OK Then
        Application.Wait Now + TimeSerial(0, 0, 1)
        MsgBox target & ": " & FileIsJpg(target)
    End If
    Kill target
End Sub

请注意,您也可以以类似的方式显式测试zip files,但我将把它作为练习留给读者。

【讨论】:

  • 感谢您的回答。我需要一段时间才能完全理解它。我会告诉你进展如何。
  • 好的,所以我将您的代码应用于我的案例。首先,我用 FileIsJpg 得到了 False,所以我的图片不是 jpg。然后我按照这篇文章 [groups.google.com/forum/#!msg/… 的建议用If header = &H5089 Then 修改了If header = &HD8FF Then,但它仍然没有通过,所以我认为它也不是png……所以这个结果如何帮助我保存我的图像?
  • @Seb - 在Set myImages = IEDoc.getElementsByTagName("img") 之后设置断点。然后在即时窗口中输入?myImages(0).href 并按[Enter]。将结果复制并粘贴到浏览器地址栏中。你得到一个有效的文件吗?
  • 所以我这样做并获得了一个有效的href,当我在浏览器中输入它时,它会将我定向到我在上面的帖子中提到的图像网页。在即时窗口中返回的 href 也显示在我上面的帖子中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多