News : The level of daily SPAM has reached insane proportions, all registrations are now manual. I ask you to send me an e-mail (john (at) murga (dot) org), to confirm that you want me to create an account for you.


Post Reply  Post Thread 
LuaSocket image
Author Message
asafp
Member
***


Posts: 81
Group: Registered
Joined: Jan 2008
Status: Offline
Reputation: 0
Post: #1
LuaSocket image

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.

This post was last modified: 07-25-2008 09:58 AM by asafp.

07-25-2008 09:52 AM
Visit this user's website Find all posts by this user Quote this message in a reply
znarf
Junior Member
**


Posts: 34
Group: Registered
Joined: Jun 2007
Status: Offline
Reputation: 0
Post: #2
RE: LuaSocket image

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.

07-25-2008 08:39 PM
Find all posts by this user Quote this message in a reply
asafp
Member
***


Posts: 81
Group: Registered
Joined: Jan 2008
Status: Offline
Reputation: 0
Post: #3
RE: LuaSocket image

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.

07-26-2008 12:07 AM
Visit this user's website Find all posts by this user Quote this message in a reply
JohnMurga
Administrator
*******


Posts: 381
Group: Administrators
Joined: Apr 2007
Status: Offline
Reputation: 2
Post: #4
RE: LuaSocket image

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

07-26-2008 05:49 AM
Visit this user's website Find all posts by this user Quote this message in a reply
asafp
Member
***


Posts: 81
Group: Registered
Joined: Jan 2008
Status: Offline
Reputation: 0
Post: #5
RE: LuaSocket image

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.

07-26-2008 12:54 PM
Visit this user's website Find all posts by this user Quote this message in a reply
asafp
Member
***


Posts: 81
Group: Registered
Joined: Jan 2008
Status: Offline
Reputation: 0
Post: #6
RE: LuaSocket image

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

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

07-31-2008 07:01 AM
Visit this user's website Find all posts by this user Quote this message in a reply
JohnMurga
Administrator
*******


Posts: 381
Group: Administrators
Joined: Apr 2007
Status: Offline
Reputation: 2
Post: #7
RE: LuaSocket image

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

07-31-2008 08:52 PM
Visit this user's website Find all posts by this user Quote this message in a reply
asafp
Member
***


Posts: 81
Group: Registered
Joined: Jan 2008
Status: Offline
Reputation: 0
Post: #8
RE: LuaSocket image

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

08-01-2008 02:31 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites

Forum Jump: