Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Function : InternetFileRead() aka UrlDownLoadToVar()


  • Please log in to reply
161 replies to this topic
SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
If ! hIO:=DllCall( LIB "InternetOpenA", Str,N, UInt,[color=red]4[/color], Str,N, Str,N, UInt,0 ) ; Line #5

Please try again after changing 4 to 0

Elesar
  • Members
  • 696 posts
  • Last active: May 08 2015 09:51 PM
  • Joined: 28 Jun 2007
After changing 4 to 0, I still have examples 1&2 work and 3 fails.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Does 1 work for FTP?

Elesar
  • Members
  • 696 posts
  • Last active: May 08 2015 09:51 PM
  • Joined: 28 Jun 2007
With
If ! hIO:=DllCall( LIB "InternetOpenA", Str,N, UInt,[color=red]1[/color], Str,N, Str,N, UInt,0 ) ; Line #5
FTP functions, but the first two examples time out.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
For now, I will leave 4 as default and allow an additional parameter.
I will post a rewritten version shortly..

Thanks for all the testing.

Elesar
  • Members
  • 696 posts
  • Last active: May 08 2015 09:51 PM
  • Joined: 28 Jun 2007
Not a problem, and thank you for all the great things that you post, though I don't understand all of them yet, I am learning from them :D

schnarkle
  • Members
  • 50 posts
  • Last active: Oct 07 2015 03:07 PM
  • Joined: 21 May 2007
How about links that require a username and password? Any way we could pass that info through?

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
you could always try pass them in a query string
Never lose.
WIN or LEARN.

schnarkle
  • Members
  • 50 posts
  • Last active: Oct 07 2015 03:07 PM
  • Joined: 21 May 2007
Hi Guys,

I'm trying to download a series of date-named files (1 weeks worth) to a folder on my desktop called comicsdwn.

The script acts like it is downloading the correct files and names them correctly but some of the actual comics are duplicates.

any ideas?


#SingleInstance Force
; F MINUS Comic
IniRead, n, %A_Desktop%\comicsdwn\temporary.ini, Loop, Index, 1
FileDelete, %A_Desktop%\comicsdwn\temporary.ini
While n < 8
{
	FormatTime, Date, %stamp%, yyMMdd
	InternetFileRead(data, "http://www.fullertonmasonry.com/test/fm" Date ".tif")
	VarZ_Save(data, A_Desktop "\comicsdwn\fm" Date ".tif")		
	IniWrite, % n+=1, %A_Desktop%\comicsdwn\temporary.ini, Loop, Index
	stamp+=1, days
}

	 
; AHK will automatically unload libraries on exit. If you are particular, here is a method
; to unload Wininet library without a handle.
DllCall( "FreeLibrary", UInt,DllCall( "GetModuleHandle", Str,"wininet.dll") )
Return ;                                                 // end of auto-execute section //


InternetFileRead( ByRef V, URL="", RB=0, bSz=1024, DLP="DLP", F=0x84000000 ) {
 Static LIB="WININET\", QRL=16, CL="00000000000000", N=""
 If ! DllCall( "GetModuleHandle", Str,"wininet.dll" )
      DllCall( "LoadLibrary", Str,"wininet.dll" )
 If ! hIO:=DllCall( LIB "InternetOpenA", Str,N, UInt,4, Str,N, Str,N, UInt,0 )
   Return -1
 If ! (( hIU:=DllCall( LIB "InternetOpenUrlA", UInt,hIO, Str,URL, Str,N, Int,0, UInt,F
                                                            , UInt,0 ) ) || ErrorLevel )
   Return 0 - ( !DllCall( LIB "InternetCloseHandle", UInt,hIO ) ) - 2
 If ! ( RB  )
 If ( SubStr(URL,1,4) = "ftp:" )
    CL := DllCall( LIB "FtpGetFileSize", UInt,hIU, UIntP,0 )
 Else If ! DllCall( LIB "HttpQueryInfoA", UInt,hIU, Int,5, Str,CL, UIntP,QRL, UInt,0 )
   Return 0 - ( !DllCall( LIB "InternetCloseHandle", UInt,hIU ) )
            - ( !DllCall( LIB "InternetCloseHandle", UInt,hIO ) ) - 4
 VarSetCapacity( V,64 ), VarSetCapacity( V,0 )
 SplitPath, URL, FN,,,, DN
 FN:=(FN ? FN : DN), CL:=(RB ? RB : CL), VarSetCapacity( V,CL,32 ), P:=&V,
 B:=(bSz>CL ? CL : bSz), TtlB:=0, LP := RB ? "Unknown" : CL,  %DLP%( True,CL,FN )
 Loop {
       If ( DllCall( LIB "InternetReadFile", UInt,hIU, UInt,P, UInt,B, UIntP,R ) && !R )
       Break
       P:=(P+R), TtlB:=(TtlB+R), RemB:=(CL-TtlB), B:=(RemB<B ? RemB : B), %DLP%( TtlB,LP )
       Sleep -1
 } TtlB<>CL ? VarSetCapacity( T,TtlB ) DllCall( "RtlMoveMemory", Str,T, Str,V, UInt,TtlB )
  . VarSetCapacity( V,0 ) . VarSetCapacity( V,TtlB,32 ) . DllCall( "RtlMoveMemory", Str,V
  , Str,T, UInt,TtlB ) . %DLP%( TtlB, TtlB ) : N
 If ( !DllCall( LIB "InternetCloseHandle", UInt,hIU ) )
  + ( !DllCall( LIB "InternetCloseHandle", UInt,hIO ) )
   Return -6
Return, VarSetCapacity(V)+((ErrorLevel:=(RB>0 && TtlB<RB)||(RB=0 && TtlB=CL) ? 0 : 1)<<64)
}
;  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; The following function is an add-on to provide "Download Progress" to InternetFileRead()
; InternetFileRead() calls DLP() dynamically, i.e., will not error-out if DLP() is missing
;  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

DLP( WP=0, LP=0, Msg="" ) {
 If ( WP=1 ) {
 SysGet, m, MonitorWorkArea, 1
 y:=(mBottom-46-2), x:=(mRight-370-2), VarSetCapacity( Size,16,0 )
 DllCall( "shlwapi.dll\StrFormatByteSize64A", Int64,LP, Str,Size, UInt,16 )
 Size := ( Size="0 bytes" ) ? N : "«" Size "»"
 Progress, CWE6E3E4 CT000020 CBF73D00 x%x% y%y% w370 h46 B1 FS8 WM700 WS400 FM8 ZH8 ZY3
         ,, %Msg%  %Size%, InternetFileRead(), Tahoma
 WinSet, Transparent, 210, InternetFileRead()
 } Progress,% (P:=Round(WP/LP*100)),% "Memory Download: " wp " / " lp " [ " P "`% ]"
 IfEqual,wP,0, Progress, Off
}
;  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; The following function is a part of: VarZ 46L - Native Data Compression
; View topic : www.autohotkey.com/forum/viewtopic.php?t=45559
;  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

VarZ_Save( byRef V, File="" ) { ;   www.autohotkey.net/~Skan/wrapper/FileIO16/FileIO16.ahk
Return ( ( hFile :=  DllCall( "_lcreat", Str,File, UInt,0 ) ) > 0 )
                 ?   DllCall( "_lwrite", UInt,hFile, Str,V, UInt,VarSetCapacity(V) )
                 + ( DllCall( "_lclose", UInt,hFile ) << 64 ) : 0
}


tov
  • Guests
  • Last active:
  • Joined: --
This looks great, I can see how to add this to my script and it will make it perfect.

one quick question .... any way to add a 'cancel' button to the download ?
I may have a need to do that

Thanks

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

one quick question .... any way to add a 'cancel' button to the download ?


Quick answer:
'Cancel' facility can be provided, but 'Cancel button' is not possible in AHK Progress. You will have to use a GUI for that.

tov
  • Guests
  • Last active:
  • Joined: --
Thanks SKAN
Thinking about it.. the file is quite small, so it's downloaded very fast.

I'm looking to use this to automate the backup of my website. daily backups are created to zip files, with this running timed to download the backups.

To stop multiple files building up on my server, is there anyway this can download the file and then delete it from the server ?

I had been creating this with another FTP DLL AHK Script, but I like your progress bar :)

Thanks

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

To stop multiple files building up on my server, is there anyway this can download the file and then delete it from the server ?


This function is basically meant for HTTP.. You will be better-off using the FTP wrapper.

tov
  • Guests
  • Last active:
  • Joined: --

To stop multiple files building up on my server, is there anyway this can download the file and then delete it from the server ?


This function is basically meant for HTTP.. You will be better-off using the FTP wrapper.


Thanks.. shame as I like your download bar :)

TOV
  • Guests
  • Last active:
  • Joined: --
I've managed to get this working :D

But I'd like to changed a couple of bits and need a little help !

I have 2 values:
IPA - which is the IP Address
DFileName - Downloaded file name

; Example 3, Download a FTP file: EditPlus 3.11 Evaluation Version (1 MB) and save it.
[color=red]URL := "ftp://ftp.editplus.com/epp311_en.exe"[/color]
If ( InternetFileRead( binData, URL ) > 0 && !ErrorLevel )
[color=red]    If VarZ_Save( binData, DFileName ) {[/color]
         Sleep 500
         DLP( False ) ; or use Progress, off
       }
DFileName seems to work OK as above.
How do I change the URL line to display ftp://IPA/file.txt

Also how do I get the progress bar to display DFileName
DLP( WP=0, LP=0, Msg="" ) {
 If ( WP=1 ) {
 SysGet, m, MonitorWorkArea, 1
 y:=(mBottom-46-2), x:=(mRight-370-2), VarSetCapacity( Size,16,0 )
 DllCall( "shlwapi.dll\StrFormatByteSize64A", Int64,LP, Str,Size, UInt,16 )
 Size := ( Size="0 bytes" ) ? N : "«" Size "»"
 Progress, CWE6E3E4 CT000020 CBF73D00 x%x% y%y% w370 h46 B1 FS8 WM700 WS400 FM8 ZH8 ZY3
         ,, %Msg%  %Size%, InternetFileRead(), Tahoma
 WinSet, Transparent, 210, InternetFileRead()
 } Progress,% (P:=Round(WP/LP*100)),% "Memory Download: " wp " / " lp " [ " P "`% ]"
 IfEqual,wP,0, Progress, Off
}
I've tried replacing %Msg% with %DFileName%, but that didn't work !

Any ideas ? Thanks :)