3

Lua beginner here. :)

I am trying to load a file by url and somehow I am just too stupid to get all the code samples here on SO to work for me.

How to download a file in Lua, but write to a local file as it works

downloading and storing files from given url to given path in lua

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

local file = ltn12.sink.file(io.open('test.jpg', 'w'))
http.request {
    url = 'http://pbs.twimg.com/media/CCROQ8vUEAEgFke.jpg',
    sink = file,
}

my program runs for 20 - 30s and afterwards nothing is saved. There is a created test.jpg but it is empty. I also tried to add w+b to the io.open() second parameter but did not work.

Community
  • 1
  • 1
sceiler
  • 1,145
  • 2
  • 20
  • 35

1 Answers1

9

The following works:

-- retrieve the content of a URL
local http = require("socket.http")
local body, code = http.request("http://pbs.twimg.com/media/CCROQ8vUEAEgFke.jpg")
if not body then error(code) end

-- save the content to a file
local f = assert(io.open('test.jpg', 'wb')) -- open in "binary" mode
f:write(body)
f:close()

The script you have works for me as well; the file may be empty if the URL can't be accessed (the script I posted will return an error in this case).

Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56
  • Thanks, I tried it but I got a timeout. Test.lua:4: timeout stack traceback: [C]: in function 'error' ...i\Downloads\ZeroBraneStudio\myprograms\Autonomy\Test.lua:4: in main chunk [C]: at 0x00402a57 – sceiler Apr 15 '15 at 15:58
  • This means some sort of connectivity issue, but not a problem with the Lua script. Try accessing the same URL from a browser on the same computer and you are likely to get the same error. – Paul Kulchenko Apr 15 '15 at 16:26
  • I tried to but there is no problem opening it in my browser (chrome). – sceiler Apr 15 '15 at 16:33
  • I don't have a good explanation as the script works for me and the URL is valid. Maybe outgoing requests from your script are blocked by the firewall? Try to disable the firewall for outgoing calls or try running the script from a different computer. – Paul Kulchenko Apr 15 '15 at 17:46
  • Ok, I did that. There is no timeout anymore but whether I use your or my script the saved .jpg is only 55kb big but the image cannot be viewed. If I try to view it Windows displays an error saying that it seems like it is damaged or corrupted. Did you get a perfectly fine image which is viewable? – sceiler Apr 15 '15 at 19:29
  • if you are on Windows use "wb" to save the file as binary as you probably end up with broken file because of end-of-line conversion. I updated the example. – Paul Kulchenko Apr 15 '15 at 20:09
  • I tried the update the above so it works with an image hosted on a https site, ````local https = require "ssl.https" --luasec local body, code = https.request("https://u.cubeupload.com/jbcooper/16146313918060.jpg") print(code) if not body then error(code) end local f = assert(io.open('mnt/nas/webtest2.jpg', 'wb')) -- open in "binary" mode f:write(body) f:close() ```` but it didnt work, no code, nothing. what have I missed ? – nodecentral Sep 19 '22 at 22:48
  • You should get an error message as the second returned value if the first one is `nil`. – Paul Kulchenko Sep 19 '22 at 23:53
  • Nope, thats what i thought but i get nothing.. very strange. – nodecentral Sep 20 '22 at 19:35