Download with Sockets

Asked

Viewed 154 times

1

I’m using the Luasocket and wanted to download files with it, but I don’t know how, I managed to download text file like this:

local file = io.open("Blabla.txt", "w")
file:write(tostring(http.request("Http://pokestage.ddns.net/patch/News.txt")))
file:close()
io.write("Downloaded!\n")
io.read()

How could I download files of other formats? (exe, rar...)

@Edit: I tried to:

arq = http.request("http://pokestage.ddns.net/patch/1.7z")
for ind, val in pairs(arq) do
print(ind)
end

Returned: Table expected, got string.. But when I try to write, it still doesn’t work.

  • Did you try and failed? What’s the problem. I never used Luasocket but in theory it is for her to be able to download anything.

  • Try, he "downloaded" the file, but it’s corrupted. I tried this file: http://pokestage.ddns.net/patch/1.7z

  • I’m not here to test. Maybe your problem is in the recording. Anyway this way does not compile. http://answall.com/help/mcve. You have read the documentation, there is probably an HTTP header before the file, you would need to separate this part. Check the contents of the files you downloaded header.

  • Edit on the question what I tried.

  • This second code doesn’t seem to make any sense to me. Anyway, I’ve already hinted what the problem might be.

  • I don’t understand what you meant in your explanation '-'

  • You can put the first lines of the file here Blabla.txt?

  • There is nothing but archive content. -> 30/10/2014 <-

Show 3 more comments

1 answer

3


I was not getting it because I was opening a binary file in text mode. When I changed "w"(write) to "Wb" (write Binary), I did it. It was like this:

local file = io.open("1.7z", "wb")
file:write(tostring(http.request("Http://pokestage.ddns.net/patch/1.7z")))
file:close()
io.write("Downloaded!\n")
io.read()

Exactly what Maniero said.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.