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

Active Member
Licensed 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

Administrator
Staff member
Licensed 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

Active Member
Licensed 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

Active Member
Licensed 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 0
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
Top