wax.http's binary download question

17 views
Skip to first unread message

naldikt

unread,
Apr 8, 2010, 3:54:08 PM4/8/10
to iPhone Wax
Hi Corey,

I am stuck when trying to use wax.http to download a file (in this
case it's a .doc file); here's the code:

function onDone(body,res)
puts("")
puts("res is ")
puts(res)
puts("body is ")
puts(body)

local path = NSDocumentDirectory .. "/" .. "example.doc"
puts("path is " .. path)
local myFile = io.open( path, "w+b" )
myFile:write( tostring(body) )
myFile:close()

end
local targetURL = "http://naldi.olearn.com/example/doc.doc"
wax.http.request{targetURL,format="text",callback=onDone}


Feel free to use the targetURL; it's working now. If you try to
download it using web browser, it will download the correct doc file
and you can open it.

My problems:
[1] Using the code above, the body is "nil". It returns "nil"
also when the format is not specified.
[2] If I change the wax.http.request's format to "binary", it does
show something, but when i tried to write it to a file it complained
that io.write needed a string input, so i converted it to string but
still couldn't open it because it tried to save the binary as string.
I guess my question is, how can I save a binary to a file?
[3] What is the default "format" value for wax.http.request?


Regards,
Naldi

PS: If I use lua's socket.http instead of wax's, the code above (with
little modification to fit lua's socket.http format of course), works
just fine and I can open it.

corey johnson

unread,
Apr 8, 2010, 4:19:10 PM4/8/10
to ipho...@googlegroups.com
[1] When you download a file, wax looks at the responses
"Content-Type" http header. In this case it is "application/msword"
and Wax doesn't know how to deal with it so that is why you need to
specify the binary format.

[2] When you get the binary back, it's stored as NSData. The easiest
way to save that to a file is like this:

local data, res =
wax.http.get{"http://naldi.olearn.com/example/doc.doc",
format="binary"}
data:writeToFile_atomically(NSDocumentDirectory .. "/something.doc", true)

[3] The default format is text


--
Subscription settings: http://groups.google.com/group/iphonewax/subscribe?hl=en
Reply all
Reply to author
Forward
0 new messages