PDA

View Full Version : How to download a file with LUA



mhund
August 6th, 2016, 06:24 PM
Hello,

maybe there is an easy solution but I cannot find it: I want to download a file from a server (via simple http download) and save locally on the file system - with lua code. Does girder lua provide something which does the job?

thanks for some good ideas.

Martin

mhund
August 8th, 2016, 06:04 AM
I see that with earlier versions of girder there was a call like win.URLDownloadToFile but it seems to be gone. Is there something similar today? It is not about a text file but an image. So I cannot use the normal text based io calls.

mhund
August 8th, 2016, 07:40 AM
seems to be holiday time :-) I have to help myself. I wrote this:



require("socket.http")
require("ltn12")

testfile = io.open("C:/localpath/test.png", "wb")

socket.http.request{
url = "http://url.server.net/picture.png",
sink = ltn12.sink.file(testfile)
}
testfile = nil


not sure if it is smart code but it seems to work. If there is a better way, please let me know.

Ron
August 8th, 2016, 12:25 PM
Sorry we just moved from FL back to CA. It was a crazy two weeks and we're only half way back up.

Your code is fine or you could use the built-in network.get function:


network.get( "http://www.promixis.com", function ( success, status, body )

if not success then
print("Sorry could not connect with server")
return
end

if status ~= 200 then
print("Sorry server returned", status)
return
end

print(body)

local out = assert(io.open( "image.jpg", "wb"))
out:write(body)
out:close()
end, 3000)

mhund
August 9th, 2016, 07:41 AM
Thank you Ron. Good luck with your new location!

Is this code you are providing also working with binary files or only with text files?

Ron
August 9th, 2016, 09:30 AM
that should do binary as well.