May 25, 2019, 11:12:47 AM

Author Topic: read a txt file from and FTP ?  (Read 194 times)

Offline fielder

  • Jr. Member
  • **
  • Posts: 68
read a txt file from and FTP ?
« on: March 11, 2019, 07:51:25 PM »
it's possibile to read (just read) a file on an FTP server using readfile / readstream?
something like:

ReadStream("ftp::username:password:ftp.server.net/file.txt")

or i need to use an FTP module to access the file?

Offline Derron

  • Hero Member
  • *****
  • Posts: 2088
Re: read a txt file from and FTP ?
« Reply #1 on: March 11, 2019, 10:24:44 PM »
You might have a look at Brucey's bah.curl.
With default modules / "brl.stream" it is not possible.


bye
Ron

Offline fielder

  • Jr. Member
  • **
  • Posts: 68
Re: read a txt file from and FTP ?
« Reply #2 on: March 12, 2019, 07:57:56 AM »
Thank you Ron

Offline Qube

  • Administrator
  • Hero Member
  • *****
  • Posts: 1828
Re: read a txt file from and FTP ?
« Reply #3 on: March 12, 2019, 06:15:59 PM »
One of our members "decag" emailed me the below. Not sure why he sent it to my email address though :P

Ciao

io ho una sorta di 'libreria' per il modulo CURL... solo per semplificare alcune operazioni (ripetitive)

Con l'esempio sotto riportato (cambia ovviamente FTP, user e password) posso 'caricare' e 'scaricare' un determinato file (in questo caso un file con estensione bak).
Non penso ci siano differenze con file audio o di testo (forse la dimensione e qualche rischio per il buffer)

SuperStrict
Framework BaH.libcurl
Import BRL.StandardIO
Import BRL.FileSystem

Include "../lib/lib_curl2.bmx"

CURL_start()

CURL_SetUpload("t1.bak") 'what I want to upload to the server
'CURL_Upload(ftp_server_destination_path,user,password)
'CURL_Download(ftp_server_source_path,save_name,user,password)
CURL_Upload("ftp.graphio.net/temp/t1.bak","user_","****")
'CURL_Download("ftp.graphio.net/download/check.png","check.png"," user_  ","****")
CURL_Download("ftp.graphio.net/temp/t1.bak","t1-download.bak"," user_  ","****")


CURL_Info()

End  ' Fini!

Appena testato e sembra funzionare!

Ciao, fammi sapere se รจ stato utile :)

Code: BlitzMax
  1. Rem
  2.  
  3.                                 LIB CURL
  4.                                 comandi veloci
  5.                                
  6.                                 lib_curl2.bmx
  7.                                
  8.                                 v.1.0   13.02.2017
  9.                                
  10.                                
  11.  
  12. End Rem
  13.  
  14. Global curl:TCurlEasy = TCurlEasy.Create()
  15. Global curl_uploadhelper:UploadHelper
  16. Rem
  17. curl.setOptInt(CURLOPT_FOLLOWLOCATION, 1)
  18. curl.setWriteStream(out_stream)                 'set where to write the file
  19. curl.setProgressCallback(progressCallback) ' set the progress callback function
  20. Local url:String="http://www.graphio.net/download/"+filename
  21. curl.setOptString(CURLOPT_URL, url)
  22. curl.setOptInt(CURLOPT_RESUME_FROM,fsize)       'set FROM where to restart the download
  23. Local res:Int = curl.perform()
  24. curl.cleanup()
  25. If out_stream CloseFile out_stream
  26. End Rem
  27.  
  28. Function CURL_Info:Int()
  29.         If curl
  30.                 Print "CURL INFO"
  31.                
  32.                 Local t:TCurlInfo=curl.GetInfo()
  33.                 Print "Connect time : "+t.ConnectTime()
  34.         Else
  35.                 Print "CURL INFO - not available"
  36.         End If
  37. Return 0
  38.  
  39. End Function
  40.  
  41. Function CURL_Download:Int(source$,dest$,user$,password$)
  42.         Print "[CURL_DOWNLOAD]"
  43.         Print "Source <"+source+">   -    Dest <"+dest+">"
  44.         If curl=Null CURL_start();Print "(Curl Start!)"
  45.  
  46.         If FileType(dest)<>0
  47.                 Print "Warning, file does already exists!"
  48. '               Return -1      
  49.         End If
  50.         Local out_stream:TStream=WriteFile(dest)
  51.        
  52.         If out_stream=Null
  53.                         Print "OUT_STREAM:NULL"
  54.                         Return -1
  55.         End If
  56.        
  57.        
  58.        
  59.         curl.setOptString(CURLOPT_USERPWD, user+":"+password)
  60.         curl.setOptInt(CURLOPT_FOLLOWLOCATION, 1)
  61.         curl.setWriteStream(out_stream)                 'set where to write the file
  62.         curl.setOptString(CURLOPT_URL, source)
  63.         curl.setWriteStream(out_stream)
  64.        
  65.        
  66.        
  67.         Local res:Int=Curl.Perform()
  68.         If res Print "***** " + CurlError(res) + " *****"
  69.        
  70.         curl_info()
  71.        
  72.        
  73.        
  74.         curl.cleanup()
  75.         curl=Null
  76.         If out_stream CloseFile out_stream             
  77.         Return 0
  78.  
  79. End Function
  80.  
  81. Function CURL_Upload:Int(ftp$,user$,password$="")
  82.         If curl_uploadhelper=Null
  83.                 Print "Attenzione! File da caricare non definito (UPLOAD=NULL)"
  84.                 Return -1      
  85.         End If
  86.        
  87.         Print "[CURL_UPLOAD]"
  88.        
  89.         If curl=Null CURL_start();Print "(Curl Start!)"
  90.  
  91.        
  92.         curl.setOptString(CURLOPT_URL, ftp)
  93.         curl.setOptString(CURLOPT_USERPWD, user+":"+password)
  94.         curl.setOptInt(CURLOPT_UPLOAD, True)
  95.         ' set the upload size
  96.         curl.setOptLong(CURLOPT_INFILESIZE_LARGE, curl_uploadhelper.size)
  97.         ' set the callback
  98.         curl.setReadCallback(curl_uploadhelper.Read, curl_uploadhelper)
  99.         ' do the UPLOAD !
  100.         Local res:Int = curl.perform()
  101.         If res Then
  102.                 Print "***** " + CurlError(res) + " *****"
  103.         End If
  104.         curl_uploadhelper.cleanup()
  105.         curl.cleanup()
  106.         curl=Null
  107.         Return 0
  108.  
  109. End Function
  110.  
  111. Function CURL_SetUpload:Int(filename$="")
  112.         If filename="" Return -1
  113.         curl_uploadhelper=UploadHelper.Create(filename$)
  114.  
  115. End Function
  116.  
  117. Function CURL_start:Int(_verbose:Int=1)
  118.                 curl:TCurlEasy = TCurlEasy.Create()
  119.                 curl.setOptInt(CURLOPT_VERBOSE, 1)
  120.                 'curl.setDebugCallback(CURLdebugFunction)
  121.                 curl.setDebugCallback(CURLdebugFunction)
  122.                 Return 0
  123. End Function
  124.  
  125. Function CURLdebugFunction:Int(data:Object, msgType:Int, message:String)
  126.  
  127.         ' we only care about server info
  128.         If msgType = CURLINFO_HEADER_IN Then
  129.                 Print message
  130.         End If
  131.  
  132. End Function
  133.  
  134. Type UploadHelper
  135.  
  136.         Field name:String
  137.         Field size:Int
  138.         Field currentPos:Int
  139.        
  140.         Field stream:TStream
  141.  
  142.         Function Create:UploadHelper(file:String)
  143.                 Local this:UploadHelper = New UploadHelper
  144.                
  145.                 this.name = file
  146.                 this.size = FileSize(file)
  147.                 Print "File : <"+file+"> Size: <"+this.size+">"
  148.                 Return this
  149.         End Function
  150.        
  151.         Method DoRead:Int(BUFFER:Byte Ptr, size:Int)
  152.                 If Not stream Then
  153.                         stream = ReadStream(name)
  154.                         currentPos = 0
  155.                 End If
  156.                
  157.                 Local count:Int = Stream.Read( BUFFER, size )
  158.                 currentPos :+ count
  159.                
  160.                 Return count
  161.         End Method
  162.  
  163.         Function Read:Int(BUFFER:Byte Ptr, size:Int, data:Object)
  164.                 Return UploadHelper(data).DoRead(BUFFER, size)
  165.         End Function
  166.        
  167.         Method CleanUp:Int()
  168.                 If stream CloseFile stream
  169.                 Return 0
  170.         End Method
  171.  
  172. End Type
  173.  
Until the next time...

Offline fielder

  • Jr. Member
  • **
  • Posts: 68
Re: read a txt file from and FTP ?
« Reply #4 on: March 13, 2019, 07:16:48 AM »
O wow thank you!