Results 1 to 8 of 8

Thread: Why isn't this code working

  1. #1

    Thread Starter
    Lively Member feneck's Avatar
    Join Date
    Mar 2007
    Location
    Australia, Queensland, Gympie
    Posts
    85

    Unhappy Why isn't this code working

    Dose anyone know why this code only downloads a 1kb file. I'm running Windows Vista, maybe that has something to do with it eh?

    Code:
    Private Sub Command2_Click()
     Dim b() As Byte
    
     b() = Inet1.OpenURL("http://www.whatever.com/image.jpg", icByteArray)
     
     Open App.Path & "\Image.jpg" For Binary As #1
     Put #1, 1, b
     Close #1
     
     Picture1.Picture = LoadPicture(App.Path & "\Image.jpg")
    End Sub
    Thanks for your help

    REMEMBER TO RATE

  2. #2
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: Why isn't this code working

    It try renaming the file downloaded from .jpg to .txt and opening it. I am guessing that there will be some source code in there from the webpage. That means that you will need a different link to the image.

  3. #3
    Lively Member
    Join Date
    Feb 2008
    Location
    Denmark
    Posts
    113

    Re: Why isn't this code working

    could it be that he the b is a array'er and have to go tru the. like
    Code:
    Private Sub Command2_Click()
     Dim b() As Byte
     dim i as integer
    
     b() = Inet1.OpenURL("http://www.whatever.com/image.jpg", icByteArray)
     
     Open App.Path & "\Image.jpg" For Binary As #1
     for i=0 to ubound(b)
       Put #1, i, b(i)
     next i
     Close #1
     
     Picture1.Picture = LoadPicture(App.Path & "\Image.jpg")
    End Sub
    Im not sure havent tested or anything, was just thinking ^^

  4. #4
    Hyperactive Member jokerfool's Avatar
    Join Date
    Dec 2006
    Location
    Gold Coast, Australia
    Posts
    452

    Re: Why isn't this code working

    Im no genius here, but i've come across on some servers that a image.jpg and a Image.jpg can be 2 different files, even though both the same name.

    So maybe changing everything to image.jpg.

  5. #5
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: Why isn't this code working

    Code:
    Dim b() As Byte
    Dim fnum As Long
    b() = Form1.Inet1.OpenURL("http://www.whatever.com/image.jpg", icByteArray)
            fnum = FreeFile
            Open "C:\Picture.jpg" For Binary Access Write As fnum
            Put fnum, , b()
            Close fnum
    Edit: Your problem was the "Put" line. There shouldnt be a number between the commas in this case.
    Last edited by 03myersd; May 26th, 2008 at 08:19 AM.

  6. #6

    Thread Starter
    Lively Member feneck's Avatar
    Join Date
    Mar 2007
    Location
    Australia, Queensland, Gympie
    Posts
    85

    Re: Why isn't this code working

    Good idea's everyone, but its still not working. It's only creating 1KB files with no image at all

    REMEMBER TO RATE

  7. #7
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Why isn't this code working

    You sure the image isn't being blocked, can you view it in a web browser?
    Have you tried using API code like URLDownloadToFile instead?

    Heres something I wrote awhile back. Note: It only downloads the file to the current app.path if it doesn't already exsist.

    Code:
    Option Explicit
    
    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 Sub DownloadFile(URL As String, SaveAsFile As String)
        URLDownloadToFile 0, URL, SaveAsFile, 0, 0
    End Sub
    
    Private Function bFileExists(sFile As String) As Boolean
        On Error Resume Next
        bFileExists = ((GetAttr(sFile) And vbDirectory) = 0)
    End Function
    
    Private Function RemoveURLPath(ByVal FullPath As String) As String
        Dim Pos As Long
        Pos = InStrRev(FullPath, "/")
        If Pos > 0 Then
            RemoveURLPath = Mid$(FullPath, Pos + 1)
        End If
    End Function
    
    Private Sub Command1_Click()
        Dim sURL As String, SaveAs As String
       
        sURL = "http://img141.imageshack.us/img141/7305/10771078mu5.gif"
        SaveAs = RemoveURLPath(sURL)    
        If bFileExists(SaveAs) = False Then DownloadFile sURL, SaveAs
        If bFileExists(SaveAs) = True Then Picture1.Picture = LoadPicture(SaveAs)
       
    End Sub

  8. #8

    Thread Starter
    Lively Member feneck's Avatar
    Join Date
    Mar 2007
    Location
    Australia, Queensland, Gympie
    Posts
    85

    Re: Why isn't this code working

    I'm at work currently so I can check out your code and see if it works until I get home, but I do know that the image isn't being blocked. I can get to it via browser...

    REMEMBER TO RATE

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width