This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Astrophylite's profile picture

Downloading Remote Files using HTTP

Started by Astrophylite, 21 July 2015 - 08:37 AM
Astrophylite #1
Posted 21 July 2015 - 10:37 AM
Hi,

I found a topic already about this but it wasn't clear how to use it.
I am trying to make a program that will download the files. I have got to the point where it is successfully connecting to the website and downloading and saving the file but it returns as a HTML document.
Now, I do not want to use pastebin because it is easier to download from my website / DropBox.

Thanks in advance,
ZiR

EDIT: Here is the code:
Spoiler

local tArgs = {...}
local function get(url)
  write( "Connecting to the website..." )
  local response = http.get(
    url
  )
  if response then
    print("Success")
   
    local sResponse = response.readAll()
    response.close()
    return sResponse
  else
    printError( "Failed." )
  end
end
local function printUsage()
  error("Usage: "..shell.getRunningProgram().." <get> <url> <fileName>" , 0 )
end
local sCommand = tArgs[1]
if sCommand == "get" then
  if #tArgs < 3 then
    printUsage()
    return
  end
  local sURL = tArgs[2]
  local sFile = tArgs[3]
  local sPath = shell.resolve( sFile )
  if fs.exists( sPath ) then
    print( "File already exists" )
    return
  end
  local res = get( sURL )
  if res then
    local file = fs.open( sPath, "w" )
    file.write( res )
    file.close()
    print( "Downloaded as "..sFile )
  end
end
Edited on 21 July 2015 - 08:54 AM
MKlegoman357 #2
Posted 21 July 2015 - 11:03 AM
The problem is that you're giving it the link to a webpage, rather than a direct link to a certain file. You'll have to somehow find a way to get the direct link of the file. Here's something I found on Google that might help.
Astrophylite #3
Posted 21 July 2015 - 11:08 AM
Thank you for your help! Now.. my problem is with my website..
Whenever I try to download from my website, it just says "Failed." which means that it cannot connect to my website.
I am providing the correct URL aswell.

Any help will be appreciated,
ZiR
MKlegoman357 #4
Posted 21 July 2015 - 11:20 AM
Have you tried to download the file through your browser? This link gives me a 404 error: http://zircon.cf/downloads/CC_Code/test.txt
Astrophylite #5
Posted 21 July 2015 - 11:42 AM
It doesn't give me a 404.. For me it just shows a blank page ;(

EDIT: No, I cannot download it through my browser…
Edited on 21 July 2015 - 09:42 AM
jerimo #6
Posted 21 July 2015 - 04:59 PM
If your browser doesnt anage to download it I highly doubt CC ever will while that holds true