Murga-Projects Forums

Full Version: LuaSocket image
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm using luasocket HTTP to download an image (.jpg). It downloads without any error, but when I view it, it's all garbled.

I realize this is a bit off topic, but would appreciate any suggestions.
One thing that immediately comes to my mind is byte order issues. One would expect luasocket to handle them, nevertheless it might be worthwhile to check if they are correctly done for your platform (which one?) internally.
Good luck.
This is from a Windows ASP server to a Windows XP x86 machine.

Downloads fine with curl. I was thinking it was something in the request header. Tried a few different things. Always same result. Thinking it might be transferring data as ascii instead of binary. I've had those kinds of problems with FTP. It's simple to change the transfer mode using FTP. I don't know how you would control that with HTTP.

asafp Wrote:
This is from a Windows ASP server to a Windows XP x86 machine.

Downloads fine with curl. I was thinking it was something in the request header. Tried a few different things. Always same result. Thinking it might be transferring data as ascii instead of binary. I've had those kinds of problems with FTP. It's simple to change the transfer mode using FTP. I don't know how you would control that with HTTP.


Can you post the code so I can try it ?

Cheers
JohnM

Code:
function GetFile(urlin,headin,redirin,fid)
local http=require('socket.http')
local url=require('socket.url')
local ltn12 = require("ltn12")
local mime = require("mime")
local urlname=urlin
local headers={}
local redir=true
if redirin==false then
    redir=false
end
if headin==nil then
    headers = {["Useragent"]='Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11',
--             ["Content-Type"] =  "application/x-www-form-urlencoded",
--             ["Timeout"] = "1",
            ["Accept-Encoding"]="gzip,deflate",
             ["Connection"]="Keep-Alive"}
else
    headers=headin
end
local t={}
local r, c, h, w = http.request {
  method = "GET",
  headers=headers,
  url = urlname,
  redirect = redir,
--  sink = ltn12.sink.table(t)
  sink = ltn12.sink.file(fid)
}
return c, h
end
imgin='Lg638183-01.jpg'
imgurl='http://www.compucars.co.uk/carpics/'..imgin
filename='img_compucars/'..imgin
fh=io.open(filename,'w')
c=nil
status=nil
c, heads=GetFile(imgurl, nil, nil, fh)
print (c)


Here you go.

have to put a "b" after the "w" for binary mode on file open.

fh=io.open(filename,'wb')

asafp Wrote:
have to put a "b" after the "w" for binary mode on file open.

fh=io.open(filename,'wb')


I didn't get time to get home to have a play with code ... It didn't occur to me to have a look at that, but now you mention it I guess it would make a difference.

Did it fix your problem completely ?

Cheers
JohnM

Yes, it works great now. I knew it had to be something simple.
Reference URL's