Android Question FTP Upload html file [Solved]

trepdas

Active Member
Licensed User
hello good people,

the app in my project is creating a html file (text file).

Now I need to upload it to a specific folder inside my shared web hosting account.
can this be done ?

🙏
 

josejad

Expert
Licensed User
Longtime User
can this be done ?
Of course. This is B4X... you can do almost everything¡¡


 
Upvote 0

Quandalle

Member
Licensed User
If you have an available file transfer service on your server (FTP, or better SFTP) you can easily upload using B4A's adapted libraries.
( It is better to use SFTP (secure FTP) which encrypts the communication. )
see for example :
https://www.b4x.com/android/forum/threads/android-sftp-based-on-jsch-tutorial.26994/#content
https://www.b4x.com/android/forum/threads/android-ftp-tutorial.10407/#post-57940

You can also use the OkHTTP library see for example
https://www.b4x.com/android/forum/t...r-b4j-server-over-the-internet.37201/#content


Below is an example of how to set up with SFTP (Secure File Transfert Protocol).

B4X:
' use library JSCH

sub Uploadfile (localfiledir, localfinename, remotefilename)

    Dim sftp2 As SFtp

    sftp2.Initialize("sftp2", Username , UserPass , ServerAdress, 22)
    sftp2.UploadFile(localfiledir ,localfilename,remotefilename)
    wait for sftp2_UploadCompleted (ServerPath As String, success As Boolean)
    If success Then log("Success") else  log ("fail")
    sftp2.close
end sub

Sub sftp2_PromptYesNo (Message As String)
    sftp2.SetPromptResult(True)
End Sub
 
Upvote 0

trepdas

Active Member
Licensed User
Thank you so very much good people.

a index.html file was created in the file internal assets and now I want to upload it to a non existing directory in the ftp server (i.e. mkdir).


B4X:
FTP.UploadFile(File.DirInternal, "index.html", True, "/ftpserver/New_Directory01/index.html")

will it create the non existing dir automaticaly (New_Directory01) or do I need first to mkdir and then to upload?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Quandalle

Member
Licensed User
will it create the non existing dir automaticaly (New_Directory01) or do I need first to mkdir and then to upload?
the easiest way to know is to test it 😀


as mentioned by DonnManfred (referencing the Erel post) with the use of the FTP protocol on ANdroid, it is necessary to activate the PASSIVE MODE..

With the use of SFTP (Secure FTP) there is no PASSIVE mode (even if the protocols have almost the same name, they are completely different).
When accessing external servers. it is recommend to use encrypted communication. FTP is not a secure protocol, it is better to use SFTP or HTTPS
 
Upvote 0

trepdas

Active Member
Licensed User
as expected, the binary god was not kind to me at first try.
I am playing around with the code for a while not without success. :(

I created a index.html file in the internal dir sub.
now I need to create a new directory in the remote ftp server and upload that index.html inside it.

can anyone help with a working code?


B4X:
    'create index.html in internal dir
    Writer.Initialize(File.OpenOutput(File.DirInternal, "index.html" , False))
    Writer.WriteLine (FinalHtml)
    Writer.Close

Log ("index file created.")


    'connect to ftp
    FTP.Initialize("FTP", FtpServerName, 21, FtpUser, FtpPass)
    FTP.PassiveMode=True
   
   
    Dim sf As Object = FTP.SendCommand ("MKD", NewDir)
   
    Wait For (sf) FTP_CommandCompleted (Command As String, Success As Boolean, ReplyCode As Int, ReplyString As String)
   
    FTP.SendCommand("MKD", NewDir)
'    Wait For (FTP_CommandCompleted(serverpath)) complete (flag As Boolean)
'    Wait For FTP_CommandCompleted (Success As Boolean)
'  
'  
   
    FTP.UploadFile(File.DirInternal, "index.html", True, NewPath)

'    Wait For (FTP_UploadCompleted(FtpServerName)) complete (success As Boolean)
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Try:

B4X:
    Wait For (sf) FTP_CommandCompleted (Command As String, Success As Boolean, ReplyCode As Int, ReplyString As String)

    If Success Then
        Log("Directory created")
    Else
        Log("Error : " & NewDir & " : " &LastException)
    End If

What do you get in logs?
Why have you duplicated this line?
FTP.SendCommand("MKD", NewDir)
 
Upvote 0

trepdas

Active Member
Licensed User
Try:

B4X:
    Wait For (sf) FTP_CommandCompleted (Command As String, Success As Boolean, ReplyCode As Int, ReplyString As String)

    If Success Then
        Log("Directory created")
    Else
        Log("Error : " & NewDir & " : " &LastException)
    End If

What do you get in logs?


I tried the code above.
I get in the log : Error : /home4/afcom/public_html/xn--9dbne9b/ : (Exception) Not initialized

Why have you duplicated this line?
FTP.SendCommand("MKD", NewDir)
yes, I had to mark that out as part of the earlier testings...
 
Upvote 0
Top