Android Question FTP Upload Image Problem

rscheel

Well-Known Member
Licensed User
Hello, I ask for help over rising image for ftp, which is causing me some small problems, it turns out upload images to the server and if the image up correctly update a field in the database of the phone with value 1 and also updated with value 1 bd MySQL Server.

The problem is that sometimes the image does not rise up and take it as correct even if it is not raised, there will be another way to check the rise.

Here is the code that I have to see if anyone is aware of this.

B4X:
Sub SubeImagen1 'SUBE IMAGEN1 AL SERVIDOR
  
        Dim MyPath As String
        MyPath = File.DirRootExternal & "/Carga/" 'PREGUNTA SI EL DIRECTORIO EXISTE
    If File.Exists(MyPath, "") = False Then
        ToastMessageShow("La ruta no existe", True)
    Else
        s.BeginTransaction
        c = s.ExecQuery("SELECT id_registro, id_telefono, id_usuario, image_km FROM registro WHERE  id_registro <> 0 AND sync_img1 = 0 LIMIT 1")
    If c.RowCount <> 0 Then
        For i = 0 To c.RowCount-1
        c.Position = i
        'FTP.SendCommand("MKD", "/"&c.GetString("id_controlvar")) 'CREA CARPETA EN EL SERVIDOR
        ImagenSelec = c.GetString("image_km")
        IdRegistro = c.GetString("id_registro")
        FTP.UploadFile(MyPath, ImagenSelec, False, "/img/"&ImagenSelec) 'SUBE IMAGEN AL SERVIDOR
        NumImg = 1
        NumNoti = NumNoti + 1
        Next
    Else
        SubeImagen2
    End If
        s.TransactionSuccessful
        s.EndTransaction
    End If
  
End Sub
B4X:
Sub SubeImagen2 'SUBE IMAGEN2 AL SERVIDOR
  
        Dim MyPath As String
        MyPath = File.DirRootExternal & "/Carga/" 'PREGUNTA SI EL DIRECTORIO EXISTE
    If File.Exists(MyPath, "") = False Then
        ToastMessageShow("La ruta no existe", True)
    Else
        s.BeginTransaction
        c = s.ExecQuery("SELECT id_registro, id_telefono, id_usuario, image_lt FROM registro WHERE  id_registro <> 0 AND sync_img2 = 0 LIMIT 1")
    If c.RowCount <> 0 Then
        For i = 0 To c.RowCount-1
        c.Position = i
        'FTP.SendCommand("MKD", "/"&c.GetString("id_controlvar")) 'CREA CARPETA EN EL SERVIDOR
        ImagenSelec = c.GetString("image_lt")
        IdRegistro = c.GetString("id_registro")
        FTP.UploadFile(MyPath, ImagenSelec, False, "/img/"&ImagenSelec) 'SUBE IMAGEN AL SERVIDOR
        NumImg = 2
        NumNoti = NumNoti + 1
        Next
    End If
        s.TransactionSuccessful
        s.EndTransaction
    End If
  
End Sub
B4X:
Sub FTP_UploadProgress (ServerPath As String, TotalUploaded As Long, Total As Long)
    Dim upload As String
    'upload = "Uploaded " & Round(TotalUploaded / 1000) & "KB"
    upload = Round(TotalUploaded / 1000) & "KB"
    If Total > 0 Then upload = upload & " out of " & Round(Total / 1000) & "KB"
    Log(upload)
    'ToastMessageShow(upload, True)
    '=======================Notificación===============================
     notif.Initialize
     notif.Light = False
     notif.Vibrate = False
     notif.OnGoingEvent = False
     notif.Sound = False
     notif.Icon = "uploaded"
     notif.SetInfo("Carga de Imagen ", "Subiendo imagen"&" "&upload, "")
     notif.Notify(NumNoti)
     '=======================Fin Notificación===============================
End Sub
B4X:
Sub FTP_UploadCompleted (ServerPath As String, Success As Boolean)
    Log(ServerPath & ", Success=" & Success)
    If Success = False Then Log(LastException.Message)
    If Success = True Then
        If NumImg == 1 Then
            s.ExecNonQuery2("UPDATE registro SET sync_img1 = ? WHERE id_registro = '" & IdRegistro & "'", Array As Object(1))
            GetData("UPDATE registro SET sync_img1 = 1  WHERE id_registro ='" & IdRegistro & "' AND sync_img1 = 0;", "GetUPD")
        End If
        If NumImg == 2 Then
            s.ExecNonQuery2("UPDATE registro SET sync_img2 = ? WHERE id_registro = '" & IdRegistro & "'", Array As Object(1))
            GetData("UPDATE registro SET sync_img2 = 1  WHERE id_registro ='" & IdRegistro & "' AND sync_img2 = 0;", "GetUPD")
        End If
        'ToastMessageShow("Imagen subida correctammente", True)
            'notif.Vibrate = True
             'notif.Sound = True
            notif.SetInfo("Carga de imagen", "Finalizada", "")
            notif.Notify(NumNoti)
        If NumImg = 1 Then
            NumImg = 0
        SubeImagen1
        End If
      
        If NumImg == 2 Then
            NumImg = 0
        SubeImagen2
        End If
    Else
        ToastMessageShow("Subida imagen fracasada", False)
        If NumImg == 1 Then
            NumImg = 0
            SubeImagen1
        End If
        If NumImg == 2 Then
            NumImg = 0
            SubeImagen2
        End If
    End If
End Sub
Thanks for your time.
 

Erel

Administrator
Staff member
Licensed User
Do you mean that FTP_UploadCompleted is raised with Success = True and the file was not uploaded property?
 

rscheel

Well-Known Member
Licensed User
Do you mean that FTP_UploadCompleted is raised with Success = True and the file was not uploaded property?
That's right, the real answer and FTP sends the file was never uploaded, does not always happen but there are times that happens, it seems strange.
 

rscheel

Well-Known Member
Licensed User
I think I have found the solution, I use FTP_ListCompleted to check whether the name of image that is on the phone is on the server, if this update if it is not updated in the database field.

if someone has another idea that could make me appreciate him.


B4X:
FTP.List("/img/"&ImagenSelec)
B4X:
'Codigo de prueba, solusión subida por FTP
Sub FTP_ListCompleted (ServerPath As String, Success As Boolean, Folders() As FTPEntry, Files() As FTPEntry)
    Log(ServerPath)
    If Success = False Then
        Log(LastException)
    Else
        For i = 0 To Folders.Length - 1
            Log(Folders(i).Name)
        Next
        For i = 0 To Files.Length - 1
            Log(Files(i).Name & ", " & Files(i).Size & ", " & DateTime.Date(Files(i).Timestamp))
            'ToastMessageShow(Files(i).Name & ", " & Files(i).Size & ", " & DateTime.Date(Files(i).Timestamp), False)

        If NumImg = 1 Then
            NumImg = 0
            s.ExecNonQuery2("UPDATE registro SET sync_img1 = ? WHERE image_km = '" & Files(i).Name & "'", Array As Object(1))
            GetData("UPDATE registro SET sync_img1 = 1  WHERE image_km ='" & Files(i).Name & "' AND sync_img1 = 0;", "GetUPD")
            SubeImagen1
        End If
       
        If NumImg == 2 Then
            NumImg = 0
            s.ExecNonQuery2("UPDATE registro SET sync_img2 = ? WHERE image_lt = '" & Files(i).Name & "'", Array As Object(1))
            GetData("UPDATE registro SET sync_img2 = 1  WHERE image_lt ='" & Files(i).Name & "' AND sync_img2 = 0;", "GetUPD")
            SubeImagen2
        End If   
        Next
    End If
End Sub
'Codigo de prueba, solusión subida por FTP
 
Top