Android Question ftp upload/dowload mp3 files

When I upload files to my FTP server, they are uploaded there "broken". Garbage appears instead of the expected audio track. Is this a problem with the server itself or am I doing something wrong? files are mp3 audio messages that I record using the VoiceRecording library

I would be glad for any help, thanks

I use this code for upload files:
B4X:
Sub FTP_UploadProgress (ServerPath As String, TotalUploaded As Long, Total As Long)
    Log("#########################FTP_UploadProgress")
    Dim s As String
    s = "Uploaded " & Round(TotalUploaded / 1000) & "KB"
    If Total > 0 Then s = s & " out of " & Round(Total / 1000) & "KB"
    Log(s)
End Sub

Sub FTP_UploadCompleted (ServerPath As String, Success As Boolean)
    Log("#####################FTP_UploadCompleted")
    Log(ServerPath & ", Success=" & Success)
    If Success = False Then Log(LastException.Message)
End Sub

And this one for dowload it

B4X:
Sub FTP_DownloadProgress (ServerPath As String, TotalDownloaded As Long, Total As Long)
    Dim s As String
    s = "Downloaded " & Round(TotalDownloaded / 1000) & "KB"
    If Total > 0 Then s = s & " out of " & Round(Total / 1000) & "KB"
    Log(s)
End Sub

Sub FTP_DownloadCompleted (ServerPath As String, Success As Boolean)
    Log(ServerPath & ", Success=" & Success)
    If Success = False Then Log(LastException.Message)

End Sub
 

Brian Dean

Well-Known Member
Licensed User
Longtime User
Why do you think that the problem is likely to lie in the code excerpts you have shown here? If you are making any errors then they are much more likel;y in code that is starting the uploads and downloads. Are you using Passive Mode, for example? Are you using "Wait For"?

FTP is simple and reliable for any type of data, so it is very likely that you are doing something wrong.
 
Upvote 0
Why do you think that the problem is likely to lie in the code excerpts you have shown here? If you are making any errors then they are much more likel;y in code that is starting the uploads and downloads. Are you using Passive Mode, for example? Are you using "Wait For"?

FTP is simple and reliable for any type of data, so it is very likely that you are doing something wrong.


This is probably a different problem. Now I upload and download a file in TXT format from(to) the server, and on the device I change the extension to .mp3 and now everything works.

Thanks for the answer, it's a pleasure to learn in this amazing community ^^
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Now I upload and download a file in TXT format from(to) the server, and on the device I change the extension to .mp3 and now everything works.
Sounds like the wrong solution.

" If you are making any errors then they are much more likel;y in code that is starting the uploads and downloads. "
 
Upvote 0
Hi, Erel, Thanks for the answer!)
Sounds like the wrong solution.

" If you are making any errors then they are much more likel;y in code that is starting the uploads and downloads. "

In Public Sub Initialize i have

B4X:
    ftp.Initialize(...)
    ftp.PassiveMode = True (I tried set on False too)

when I dowload files from another device I use this:
B4X:
        ftp.DownloadFile(listOfMessageValue.Get(index), False, shared, listOfMessageValue.Get(index))
Where "listOfMessageValue.Get(index)" - filename and "shared" - my folder on device where I saved audio

when I upload file from server i use this:
B4X:
      UploadFile(shared, audioMessageName, False, audioMessageName)

where audioMessageName - the name of the audio Message I generated along with it and saved into "shared" folder.

From the device from which I create an audio message it plays normally, but the "broken version" is uploaded on the server and the media player does not play this audio when I dowload it from another device. (If I upload audio in txt format everything is OK)

sorry for my english, I hope I explained it well, thanks
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Okay - I think that I know where you are going wrong. If you have had any success then you were lucky - your code will not be reliable. I suspect that you are using this tutorial which is very old (2011).

Let us look at FTP download first. When you run the statement ...
B4X:
    ftp.downloadfile(...., ...., ...., ....)
the download operation will begin. It will run asynchronously, which means that you B4A code will continue on to the following statement while the download is still in progress. If you try to read the downloaded file before the download has finished then you will see rubbish - that is what I think is happening. If there is a delay before you read the file, such as waiting for a button press, then you may be lucky and catch the file after the download is complete. But to be sure that your code will work reliably then you have to check that the download is complete before you try to read the file. The two most common ways to detect that the download operation is complete are to use a "Wait For ..." resumable sub, or put a switch in the "download complete" event. Your code samples do not show either of these, but you are still not showing very much code. The tutorial that I think you are using does not make this very clear either.

If this seems to describe your problem then let me know and I will try to give you some more guidance.
 
Last edited:
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Here are a couple of code samples that show you how to manage FTP download operations. I have tried to keep the code very simple ...
B4X:
Sub Globals
    Dim downloading, successful As Boolean
    Dim somefile, somefolder, filepath As String
End Sub

'----

Private Sub Button1_Click
    downloadFile(somefile, somefolder)
    Do While downloading
        Sleep(0)
    Loop
    If successful Then readfile(somefile, somefolder)
End Sub

Private Sub downloadFile(filename As String, folder As String)
    downloading = True
    successful = False
    ftp.DownloadFile(filepath, False, folder, filename)
End Sub

Private Sub FTP_DownloadCompleted (ServerPath As String, Success As Boolean)
    downloading = False
    successful = Success
    If Not(successful) Then    Log(LastException.Message)
End Sub


' THIS ALTERNATIVE USES "Wait For" AND CAN REPLACE THE TWO SUBROUTINES ABOVE ...

Private Sub downloadFile2(filename As String, folder As String)
    downloading = True
    ftp.downloadFile(filepath, False, folder, filename)
    Wait For FTP_DownloadCompleted(ServerPath As String, Success As Boolean)
    downloading = False
    successful = Success
End Sub
 
Upvote 1
Okay - I think that I know where you are going wrong. If you have had any success then you were lucky - your code will not be reliable. I suspect that you are using this tutorial which is very old (2011).

Let us look at FTP download first. When you run the statement ...
B4X:
    ftp.downloadfile(...., ...., ...., ....)
the download operation will begin. It will run asynchronously, which means that you B4A code will continue on to the following statement while the download is still in progress. If you try to read the downloaded file before the download has finished then you will see rubbish - that is what I think is happening. If there is a delay before you read the file, such as waiting for a button press, then you may be lucky and catch the file after the download is complete. But to be sure that your code will work reliably then you have to check that the download is complete before you try to read the file. The two most common ways to detect that the download operation is complete are to use a "Wait For ..." resumable sub, or put a switch in the "download complete" event. Your code samples do not show either of these, but you are still not showing very much code. The tutorial that I think you are using does not make this very clear either.

If this seems to describe your problem then let me know and I will try to give you some more guidance.

Thank you for your reply and for your time. The problem is more likely in the upload function, because the server receives "broken" audio. Although they are recorded normally
 
Upvote 0

johnaaronrose

Active Member
Licensed User
Longtime User
Here are a couple of code samples that show you how to manage FTP download operations. I have tried to keep the code very simple ...
B4X:
Sub Globals
    Dim downloading, successful As Boolean
    Dim somefile, somefolder, filepath As String
End Sub

'----

Private Sub Button1_Click
    downloadFile(somefile, somefolder)
    Do While downloading
        Sleep(0)
    Loop
    If successful Then readfile(somefile, somefolder)
End Sub

Private Sub downloadFile(filename As String, folder As String)
    downloading = True
    successful = False
    ftp.DownloadFile(filepath, False, folder, filename)
End Sub

Private Sub FTP_DownloadCompleted (ServerPath As String, Success As Boolean)
    downloading = False
    successful = Success
    If Not(successful) Then    Log(LastException.Message)
End Sub


' THIS ALTERNATIVE USES "Wait For" AND CAN REPLACE THE TWO SUBROUTINES ABOVE ...

Private Sub downloadFile2(filename As String, folder As String)
    downloading = True
    ftp.downloadFile(filepath, False, folder, filename)
    Wait For FTP_DownloadCompleted(ServerPath As String, Success As Boolean)
    downloading = False
    successful = Success
End Sub
Looking at post 1, it monitors a download's progress. Can you combine that progress code into either of the 2 methods above. What would the resultant code look like?
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Can you combine that progress code into either of the 2 methods above?
If I understand your question correctly, you want to be able to monitor the transfer progress. To do this you have to have an event handler to intercept the progress events. This has to be a separate routine in order to match the event signature and capture the event parameters. The code cannot be amalgamated with the code that initiates the file transfers, but this is no hardship.

The FTP_UploadProgress and FTP_DownloadProgress subroutines shown in post #1 are fine - just add them to the code samples that I used in post #7.

Let me know if I have not answered your question.
 
Upvote 0

johnaaronrose

Active Member
Licensed User
Longtime User
If I understand your question correctly, you want to be able to monitor the transfer progress. To do this you have to have an event handler to intercept the progress events. This has to be a separate routine in order to match the event signature and capture the event parameters. The code cannot be amalgamated with the code that initiates the file transfers, but this is no hardship.

The FTP_UploadProgress and FTP_DownloadProgress subroutines shown in post #1 are fine - just add them to the code samples that I used in post #7.

Let me know if I have not answered your question.
I think you've answered my question in that I've been able to compile without errors. However, to proceed further I need some answerrs. Apologies if they're off B4A topics. Am I correct in stating that the Net library handles FTP & FTPS but not SFTP? This is implied by the content of https://www.b4x.com/glossary/ftp/ Am I correct in stating that both FTP & FTPS require the FTP/FTPS user & password to be in the FTP.Initialize command. Another difference being that FTP uses port 21 (or is it port 20) in that command and FTPS uses port 990 in that command - those being the standard ports for those protocols. How do I specify 'Explicit FTPS' in the Initialize command or can it only be done by FTP.UseSSLExplicit = True after the Initialize command?
 
Last edited:
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
How do I specify 'Explicit FTPS?
I think that all of your statements are correct (FTP uses port 21) but I am not an expert and I cannot answer your question. Sorry - maybe someone else can.
 
Upvote 0
Top