Downloading files to variable or file Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
SirRFI
Posts: 404
Joined: 25 Nov 2015, 16:52

Downloading files to variable or file

31 Oct 2017, 18:37

Greetings,

I am looking for a way to download webcontents/files to variable or file. More superficially the two following cases:
• A JSON file (from FTP or something) to variable. It could be loaded in with JSON/JXON library next on.
• Another file from web (that would likely prompt downloading/saving when pasting the address in browser) to file.

I am aware of UrlDownloadToFile, which is said to use Internet Explorer - I'd like to omit IE if possible.
I stumbled upon ComObjCreate WinHttpRequest, but I don't know if it's suitable for the work, or if it uses IE.

Any suggestions?
Use

Code: Select all

[/c] forum tag to share your code.
Click on [b]✔[/b] ([b][i]Accept this answer[/i][/b]) on top-right part of the post if it has answered your question / solved your problem.
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Downloading files to variable or file  Topic is solved

31 Oct 2017, 19:06

You could try this.
download urls to vars, partially/fully, via WinHttpRequest - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 09#p168409
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
SirRFI
Posts: 404
Joined: 25 Nov 2015, 16:52

Re: Downloading files to variable or file

01 Nov 2017, 19:18

Requirements

Redistributable
WinHTTP 5.0 and Internet Explorer 5.01 or later on Windows XP and Windows 2000.
https://msdn.microsoft.com/en-us/librar ... s.85).aspx
Guess I can forget about the non-IE requirement, but it's old enough to be good. This is because I want the future project to work on WinXP SP3 and above, without additional requirements.

Code: Select all

JEE_UrlDownloadToVar(vUrl, vData, vSize)
Got some questions regarding the code:
• Is there any reason You use vData and vSize as byRef arguments, despite they are not passed into the function?
• Why not use them ↑ only inside the function and return the result of StrGet?
• Why oHTTP.ResponseBody over oHTTP.ResponseText?
oHTTP := "" - doesn't this self-destroy when function ends, like normal local variables?

I think it's made that way due to the second usage example, which is the "partial" thing. It's somewhat messy to me, as that part of code basically contains about the same content as the function, so it's all mixed together.

Nonetheless, I'll need only the first variant to download json file. Thanks.
Use

Code: Select all

[/c] forum tag to share your code.
Click on [b]✔[/b] ([b][i]Accept this answer[/i][/b]) on top-right part of the post if it has answered your question / solved your problem.
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Downloading files to variable or file

01 Nov 2017, 22:55

- vData is binary data. If you create binary data within the function, and return a pointer to it, that data gets destroyed when the function ends. In general, ByRef is one of the best solutions for handling binary data in AHK v1 and v2, although AHK v2 is making normal variables capable of handling null bytes.
- We either output vSize as the return value or as a ByRef variable.
- Simply using strings could be a problem if we have say an image file with multiple null bytes.
- ResponseBody for binary data, ResponseText for text. However, sometimes ResponseText can cause a crash I believe, something about encodings, so ResponseBody can be safer. With ResponseBody you get html which you can open as an HTMLFile object.
- It's possible that the oHTTP line is redundant. This is a tricky one, (a) for possible stricter variable handling in future, certain 'redundant' actions should take place, although this is not necessarily an example of that, (b) if someone wants to copy the code outside of the function into the body of the script, then it's better to have everything included for copy and paste.

- We try our best but functions are not always perfect, or 'perfect' is not always clear.
- Objects could provide an alternate means of achieving some of this functionality.
- AFAIK no-one had written a script in AHK to download a file (not text) to a variable, using WinHttpRequest until I asked about it. Although there had been some scripts to recreate the UrlDownloadToFile command using InternetReadFile, the same Winapi function that the command uses.

Simple Download (Bin, ToString und ToFile) - Gebrauchsfertige Skripte & Funktionen - AutoHotkey Community
https://autohotkey.com/board/topic/8919 ... nd-tofile/

Anyhow, all good questions. Cheers.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], BushMange, CEA6597, howardb1, VACO BenQ, w0z and 180 guests