Jump to content

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

UrlDownloadToFile


  • Please log in to reply
11 replies to this topic
LarryC
  • Members
  • 68 posts
  • Last active: Aug 14 2016 07:53 PM
  • Joined: 28 Oct 2012
UrlDownloadToFile command seems to have stopped working. My scripts now work, but had to follow this thread. Something happen in last few weeks?

Menixator
  • Members
  • 744 posts
  • Last active: Sep 01 2015 02:54 PM
  • Joined: 10 Jul 2013

If UrlDownloadtoFile Didin't work, Try this:


;Function by Wicked
_download_to_file(u,s){
	static r:=false,request:=comobjcreate("WinHttp.WinHttpRequest.5.1")
	if(!r||request.option(1)!=u)
		request.open("GET",u)
    ;request.SetProxy(2, "XXX.XXX.XXX.XXX:PORT") ; IF YOU NEED TO SET YOUR PROXY
	request.send()
	if(request.responsetext="failed"||request.status!=200||comobjtype(request.responsestream)!=0xd)
		return false
	p:=comobjquery(request.responsestream,"{0000000c-0000-0000-C000-000000000046}")
	f:=fileopen(s,"w")
	loop{
		varsetcapacity(b,8192)
		r:=dllcall(numget(numget(p+0)+3*a_ptrsize),ptr,p,ptr,&b,uint,8192, "ptr*",c)
		f.rawwrite(&b,c)
	}until (c=0)
	objrelease(p)
	f.close()
	return request.responsetext
}

Example:



url = http://cdn.wallwuzz.com/uploads/cars-desktop-wallpaper-wallwuzz-hd-wallpaper-652.jpg
file = C:\folder\test.jpg

_download_to_file(url, file)

And about the last part, i have no idea. happy.png



garry
  • Spam Officer
  • 3219 posts
  • Last active: Feb 06 2016 09:32 PM
  • Joined: 19 Apr 2005

urldownloadtofile works for me

here 3-examples ( ahk_L )

;- http://www.autohotkey.com/board/topic/89198-simple-download-bin-tostring-und-tofile/page-2 ---
url  := "http://www.autohotkey.com/download/AutoHotkeyInstall.exe"
SplitPath, url, name, dir, ext, name_no_ext, drive

;- Example-1 = urldownloadtofile
F1=%a_desktop%\%name_no_ext%_1.%ext%
SplashTextOn, 400, 40, ,Now downloading`n%name_no_ext%_1`nTo`nDesktop
begin1:=a_tickcount
urldownloadtofile,%url%,%f1%
delta1:=a_tickcount - begin1
SplashTextOff
msgbox, 262208,Download ,Download-1 complete`nDelta1=%delta1%


;- Example-2  function from Bentschi
;- http://www.autohotkey.com/board/topic/89198-simple-download-bin-tostring-und-tofile/
F2=%a_desktop%\%name_no_ext%_2.%ext%
SplashTextOn, 400, 40, ,Now downloading`n%name_no_ext%_2`nTo`nDesktop
begin2:=a_tickcount
DownloadToFile(url, F2)
delta2:=a_tickcount - begin2
SplashTextOff
msgbox,Download-2 complete`nDelta2=%delta2%


;- Example-3  function from Wicked
F3=%a_desktop%\%name_no_ext%_3.%ext%
begin3:=a_tickcount
SplashTextOn, 400, 40, ,Now downloading`n%name_no_ext%_3`nTo`nDesktop
_download_to_file(url,F3)
delta3:=a_tickcount - begin3
SplashTextOff
msgbox,Download-3 complete`nDelta3=%delta3%
return


;-- Function by Bentschi
DownloadToFile(url, filename)
{
    static a := "AutoHotkey/" A_AhkVersion
    if (!(o := FileOpen(filename, "w")) || !DllCall("LoadLibrary", "str", "wininet") || !(h := DllCall("wininet\InternetOpen", "str", a, "uint", 1, "ptr", 0, "ptr", 0, "uint", 0, "ptr")))
        return 0
    c := s := 0
    if (f := DllCall("wininet\InternetOpenUrl", "ptr", h, "str", url, "ptr", 0, "uint", 0, "uint", 0x80003000, "ptr", 0, "ptr"))
    {
        while (DllCall("wininet\InternetQueryDataAvailable", "ptr", f, "uint*", s, "uint", 0, "ptr", 0) && s>0)
        {
            VarSetCapacity(b, s, 0)
            DllCall("wininet\InternetReadFile", "ptr", f, "ptr", &b, "uint", s, "uint*", r)
            c += r
            o.rawWrite(b, r)
        }
        DllCall("wininet\InternetCloseHandle", "ptr", f)
    }
    DllCall("wininet\InternetCloseHandle", "ptr", h)
    o.close()
    return c
}


;Function by Wicked
_download_to_file(u,s){
	static r:=false,request:=comobjcreate("WinHttp.WinHttpRequest.5.1")
	if(!r||request.option(1)!=u)
		request.open("GET",u)
    ;request.SetProxy(2, "XXX.XXX.XXX.XXX:PORT") ; IF YOU NEED TO SET YOUR PROXY
	request.send()
	if(request.responsetext="failed"||request.status!=200||comobjtype(request.responsestream)!=0xd)
		return false
	p:=comobjquery(request.responsestream,"{0000000c-0000-0000-C000-000000000046}")
	f:=fileopen(s,"w")
	loop{
		varsetcapacity(b,8192)
		r:=dllcall(numget(numget(p+0)+3*a_ptrsize),ptr,p,ptr,&b,uint,8192, "ptr*",c)
		f.rawwrite(&b,c)
	}until (c=0)
	objrelease(p)
	f.close()
	return request.responsetext
}
;===========================================================================================



StoleYourFood
  • Members
  • 54 posts
  • Last active:
  • Joined: 03 May 2013

Uh.. here's a smaller example

UrlDownloadToFile, http://data-cdn.mbamupdates.com/tools/mbam-rules.exe, C:\Docume~1\%Username%\Desktop\mbam-rules.exe
Msgbox, Download complete


garry
  • Spam Officer
  • 3219 posts
  • Last active: Feb 06 2016 09:32 PM
  • Joined: 19 Apr 2005

yes, this is ahk command urldownloadtofile which works for me

above just examples with a_tickcount (time) , splashtext (message) , splitpath (get name)  and 2 other examples ( functions )



LarryC
  • Members
  • 68 posts
  • Last active: Aug 14 2016 07:53 PM
  • Joined: 28 Oct 2012

My question was specific to the AHK command "UrlDownloadToFile", itself, not how to work around.

 

All examples not using UrlDownloadToFile" do work, thank you very much.

 

The AHK command itself, has stopped working for me.

 

Garry's example, which he says works for him, also does not work for me. (when I change the file to C:\1\a.txt)

Worked well for years, only stopped last few weeks.???

??

 

Maybe I'm going crazy.



StoleYourFood
  • Members
  • 54 posts
  • Last active:
  • Joined: 03 May 2013

Can't you post your code?



garry
  • Spam Officer
  • 3219 posts
  • Last active: Feb 06 2016 09:32 PM
  • Joined: 19 Apr 2005

an example from user DataLife with progressbar

;-------- http://www.autohotkey.com/board/topic/17915-urldownloadtofile-progress/page-4 ---
/*
;-- from user DataLife 24.April 2013
1. ran the below script to download autohotkeyinstall.exe to the script folder.
2. disconnected from the internet. (to make it impossible to be mistaken where the file came from when the script is ran a second time)
3. deleted autohotkeyinstall.exe from the script folder.
4. ran the below script again
5. the script finished in less then 1 second and the script pulled autohotkeyinstall.exe from the cache and put it in the script folder.
*/


sUrl  := "http://www.autohotkey.com/download/AutoHotkeyInstall.exe"
;sUrl  :="http://ffmpeg.zeranoe.com/builds/win32/static/ffmpeg-latest-win32-static.7z"

SplitPath, surl, name, dir, ext, name_no_ext, drive
sFile=%a_desktop%\%name%
ifexist,%sFile%
   filedelete,%sFile%

    Gui, 3:Add, Progress, x10   y20  w450  h25  cBlue vMyProgress
    Gui, 3:Add, Edit    , x10   y50  w100  h25  vEdit11
    Gui, 3:Add, Edit    , x120  y50  w100  h25  vEdit12
    Gui, 3:Show,          x400 y400 w500  h100   ,Download
    gosub,dnl
return

esc::exitapp
3Guiclose:
exitapp


dnl:
a:=a_tickcount
;-------
VarSetCapacity(vt, 4*11), nParam = 31132253353
Loop, Parse, nParam
	NumPut(RegisterCallback("DownloadProgress", "Fast", A_LoopField, A_Index-1), vt, 4*(A_Index-1))
DllCall("urlmon\URLDownloadToFileA", "Uint", 0, "str", sUrl, "str", sFile, "Uint", 0, "UintP", &vt)
;-------
delta:=((a_tickcount-a)/1000)
;msgbox, 262208,Downloaded ,Downloaded in %delta% seconds
GuiControl,3:,Edit12, %delta% -sec
return

;------- function -----------
DownloadProgress(pthis, nProgress = 0, nProgressMax = 0, nStatusCode = 0, pStatusText = 0)
{
	If A_EventInfo = 6
        {
        p := 100 * nProgress//nProgressMax
        GuiControl,3:,MyProgress, %p%
        GuiControl,3:,Edit11, %p%
	;Progress, % p := 100 * nProgress//nProgressMax, %p%, % nProgress . " of " . nProgressMax
        }
	Return 0
}
return
;----------------------------------



LarryC
  • Members
  • 68 posts
  • Last active: Aug 14 2016 07:53 PM
  • Joined: 28 Oct 2012

StoleYourFood wrote: " post your code"

 

url = http://www.weather.c...o CAXX0504:1:CA

File = XX.TX
UrlDownloadToFile, %URL%, %File%
 
Not complicated, straight out of CHM, worked flawlessly in this and similar scripts for years.  All stopped working few weeks ago.   strange
Not a real problem, easily solved because of AHK richness, many ways to do the same thing.
Just strange.??


garry
  • Spam Officer
  • 3219 posts
  • Last active: Feb 06 2016 09:32 PM
  • Joined: 19 Apr 2005

your example works for me

I use AHK_basic  1.0.48.05 ansi   or AHK_L  1.1.13.00  ansi

script to check version

;--- versionx ansi or unicode ---------
if (A_IsUnicode)
  codex := " Encoding is Unicode"
else
  codex := " Encoding is ANSI"
aa := "Version: " ( InStr( (v:=A_AhkVersion), "1.1" ) ? "ahk_L " : "ahk_Basic " ) v  codex
msgbox, %aa%
return



LarryC
  • Members
  • 68 posts
  • Last active: Aug 14 2016 07:53 PM
  • Joined: 28 Oct 2012

Thanks garry, your effort and work is very much appreciated. Confirmed, I am crazy.



garry
  • Spam Officer
  • 3219 posts
  • Last active: Feb 06 2016 09:32 PM
  • Joined: 19 Apr 2005

 

.. Confirmed, I am crazy.

you're not, don't know why not works, but maybe can use other solutions which works ...

or also freeware commandline wget , curl ....