Murga-Projects Forums
LuaSocket image - Printable Version

+- Murga-Projects Forums (http://www.murga-projects.com/forum)
+-- Forum: Project Forums (/forumdisplay.php?fid=1)
+--- Forum: MurgaLua - General (/forumdisplay.php?fid=2)
+--- Thread: LuaSocket image (/showthread.php?tid=333)


LuaSocket image - asafp - 07-25-2008 09:52 AM

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.


RE: LuaSocket image - znarf - 07-25-2008 08:39 PM

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.


RE: LuaSocket image - asafp - 07-26-2008 12:07 AM

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.


RE: LuaSocket image - JohnMurga - 07-26-2008 05:49 AM

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


RE: LuaSocket image - asafp - 07-26-2008 12:54 PM

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.


RE: LuaSocket image - asafp - 07-31-2008 07:01 AM

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

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


RE: LuaSocket image - JohnMurga - 07-31-2008 08:52 PM

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


RE: LuaSocket image - asafp - 08-01-2008 02:31 AM

Yes, it works great now. I knew it had to be something simple.