Android Question Problem with FileOrFolderExist on FTP

WiLey2000

Member
Licensed User
I'm trying to upload a picture take with the camera, to a FTP.
In my routine, after the picture is taken, it checks the ftp directory to see if the file exists. If it does the user is asked if they want to replace it. If they answer yes, the picture is upload and replaces the existing picture. If the user answers no, it should not upload picture.
In my code below, The picture gets uploaded no matter how the user answers.
What am I doing wrong?

B4X:
Sub Start_Upload
   
    MyPath = File.DirRootExternal & sRoot & "/" & sJobNo & "/" & sReportType & "/" & sCustID
    MyFTPPath = "Inspections/" & sJobNo & "/" & sReportType & "/" & sCustID
    MyUploadFile = sDefItem & ".jpg"
   
    If (FTP.FileOrFolderExist(MyUploadFile, MyFTPPath, False)) = False Then
   
    FTP.MakeDir("Inspections")
    FTP.MakeDir("Inspections/" & sJobNo)
    FTP.MakeDir("Inspections/" & sJobNo & "/" & sReportType)
    FTP.MakeDir("Inspections/" & sJobNo & "/" & sReportType & "/" & sCustID)
        FTP.UploadFile(MyPath, MyFTPPath, MyUploadFile)

    End If
End Sub

Sub FTP_FileExist(Found As Boolean)
    Dim Answ As Int
    If Found Then
        Answ = Msgbox2("Item " & sDefItem & " already excists. Do you want to replace it?","File Excists","Yes","","No", Null)
        If (Answ = DialogResponse.POSITIVE) Then
           
                FTP.UploadFile(MyPath, MyFTPPath, MyUploadFile)

        End If
        If (Answ = DialogResponse.NEGATIVE) Then
            FTP.Close
        End If
    End If   
End Sub

Thanks, John
 

DonManfred

Expert
Licensed User
If (FTP.FileOrFolderExist
This does not work. You need to wait for the
FTP_FileExist event get raised. Here you can decide what to do.

Something like

B4X:
Sub Start_Upload
 
    MyPath = File.DirRootExternal & sRoot & "/" & sJobNo & "/" & sReportType & "/" & sCustID
    MyFTPPath = "Inspections/" & sJobNo & "/" & sReportType & "/" & sCustID
    MyUploadFile = sDefItem & ".jpg"
 
    FTP.FileOrFolderExist(MyUploadFile, MyFTPPath, False)
 

End Sub

Sub FTP_FileExist(Found As Boolean)
    Dim Answ As Int
    If Found Then
        Answ = Msgbox2("Item " & sDefItem & " already excists. Do you want to replace it?","File Excists","Yes","","No", Null)
        If (Answ = DialogResponse.POSITIVE) Then
    FTP.MakeDir("Inspections")
    FTP.MakeDir("Inspections/" & sJobNo)
    FTP.MakeDir("Inspections/" & sJobNo & "/" & sReportType)
    FTP.MakeDir("Inspections/" & sJobNo & "/" & sReportType & "/" & sCustID)
        FTP.UploadFile(MyPath, MyFTPPath, MyUploadFile)


        End If
        If (Answ = DialogResponse.NEGATIVE) Then
            FTP.Close
        End If
    End If 
End Sub
 
Upvote 0

WiLey2000

Member
Licensed User
Thanks Manfred
That works great if a file exists, but if the file does not exist it does not upload anything.

John
 
Last edited:
Upvote 0

WiLey2000

Member
Licensed User
Thanks again Manfred!

Final code looks like this:
B4X:
Sub Start_Upload
   
    MyPath = File.DirRootExternal & sRoot & "/" & sJobNo & "/" & sReportType & "/" & sCustID
    MyFTPPath = "Inspections/" & sJobNo & "/" & sReportType & "/" & sCustID
    MyUploadFile = sDefItem & ".jpg"
   
    FTP.FileOrFolderExist(MyUploadFile, MyFTPPath, False)
   
End Sub

Sub FTP_FileExist(Found As Boolean)
    Dim Answ As Int
    If Found Then
        Answ = Msgbox2("Item " & sDefItem & " already excists. Do you want to replace it?","File Excists","Yes","","No", Null)
        If (Answ = DialogResponse.POSITIVE) Then
            FTP.MakeDir("Inspections")
            FTP.MakeDir("Inspections/" & sJobNo)
            FTP.MakeDir("Inspections/" & sJobNo & "/" & sReportType)
            FTP.MakeDir("Inspections/" & sJobNo & "/" & sReportType & "/" & sCustID)
                FTP.UploadFile(MyPath, MyFTPPath, MyUploadFile)

        End If
        If (Answ = DialogResponse.NEGATIVE) Then
            FTP.Close
        End If
    Else
        FTP.UploadFile(MyPath, MyFTPPath, MyUploadFile)
    End If   
End Sub

John
 
Upvote 0
Top