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 :)
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.
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 ^^
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.
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.
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
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
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...