Android Question cant open file after ftp downlod.

alon

Active Member
Licensed User
hi ,
i am using ftp download to download files, after download is finish , i cant open the file ,because its corrupted.
but if i am browsing to the folder ,i can open it with no problem.

any ideas?

thanks.. .
 
Last edited:

RichardN

Active Member
Licensed User
@alon... I had a similar problem. My database asset on the server had been incorrectly recognised by FileZilla as an ASCII file and so corrupted on upload. It took me ages to find the cause!
 
Upvote 0

alon

Active Member
Licensed User
First thanks for your replay...

My problem is with downloading files,

the download does not give any errors.

the error is after i am trying to open the file from my app, although there is no problem to open the files manually.



B4X:
Sub FTP_DownloadCompleted (ServerPath As String, Success As Boolean)
    Starter.FTP.Close
    Log(ServerPath & ", Success=" & Success)
    If Success = False Then
        Log(LastException.Message)
    Else
        Try
           
            Dim sFileType As String 
            sFileType = ServerPath.SubString(ServerPath.Length-4)
            If sFileType.SubString2(0,1) = "." Then
                sFileType = sFileType.SubString(1)
            End If
            Dim i As Intent
            i.Initialize(i.ACTION_VIEW, "file://" & File.Combine(Starter.TargetFolder & "/",  ServerPath))
           
           
            Dim types As Map
            types.Initialize
           
            types.Put("apk","application/vnd.android.package-archive")
            types.Put("au","audio/basic")
            types.Put("avi","video/msvideo, video/avi, video/x-msvideo")
            types.Put("bmp","image/bmp")
            types.Put("bz2","Application/x-bzip2")
            types.Put("css","text/css")
            types.Put("dtd","Application/xml-dtd")
            '            types.Put("doc","Application/msword")
            types.Put("doc","application/msword")
            '            types.Put("docx","Application/vnd.openxmlformats-officedocument.wordprocessingml.document")
            types.Put("docx","application/msword")
            types.Put("dotx","Application/vnd.openxmlformats-officedocument.wordprocessingml.template")
            types.Put("es","Application/ecmascript")
            types.Put("exe","Application/octet-stream")
            types.Put("gif","image/gif")
            types.Put("gz","Application/x-gzip")
            types.Put("hqx","Application/mac-binhex40")
            types.Put("html","text/html")
            types.Put("jar","Application/java-archive")
            types.Put("jpg","image/jpeg")
            types.Put("js","Application/x-javascript")
            types.Put("midi","audio/x-midi")
            types.Put("mp3","audio/mpeg")
            types.Put("mpeg","video/mpeg")
            types.Put("ogg","audio/vorbis, Application/ogg")
            types.Put("pdf","application/pdf")
            types.Put("pl","Application/x-perl")
            types.Put("png","image/png")
            types.Put("potx","Application/vnd.openxmlformats-officedocument.presentationml.template")
            types.Put("ppsx","Application/vnd.openxmlformats-officedocument.presentationml.slideshow")
            types.Put("ppt","Application/vnd.ms-powerpointtd>")
            types.Put("pptx","Application/vnd.openxmlformats-officedocument.presentationml.presentation")
            types.Put("psv","Application/postscript")
            types.Put("qt","video/quicktime")
            types.Put("ra","audio/x-pn-realaudio, audio/vnd.rn-realaudio")
            types.Put("ram","audio/x-pn-realaudio, audio/vnd.rn-realaudio")
            types.Put("rdf","Application/rdf, Application/rdf+xml")
            types.Put("rtf","Application/rtf")
            types.Put("sgml","text/sgml")
            types.Put("sit","Application/x-stuffit")
            types.Put("sldx","Application/vnd.openxmlformats-officedocument.presentationml.slide")
            types.Put("svg","image/svg+xml")
            types.Put("swf","Application/x-shockwave-flash")
            types.Put("tar.gz","Application/x-tar")
            types.Put("tgz","Application/x-tar")
            types.Put("tiff","image/tiff")
            types.Put("tsv","text/TAB-separated-values")
            types.Put("txt","text/plain")
            types.Put("wav","audio/wav, audio/x-wav")
            types.Put("xlam","Application/vnd.ms-excel.addin.macroEnabled.12")
            types.Put("xls","application/vnd.ms-excel")
            types.Put("xlsb","Application/vnd.ms-excel.sheet.binary.macroEnabled.12")
            types.Put("xlsx","application/vnd.ms-excel")
            '            types.Put("xltx","Application/vnd.openxmlformats-officedocument.spreadsheetml.template")
            types.Put("xltx","vnd.ms-excel")
            types.Put("xml","Application/xml")
            types.Put("zip","Application/zip, Application/x-compressed-zip")
           
            If types.ContainsKey(sFileType) Then
                i.SetType(types.Get(sFileType))
            Else
                i.SetType("application/vnd.android.package-archive")
            End If
'           
           
           
           
            StartActivity(i)
        Catch
            Log(LastException)
        End Try
    End If
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Upvote 0

alon

Active Member
Licensed User
1. You don't need to have a DownloadCompleted sub. Use Wait For instead: https://www.b4x.com/android/forum/threads/84821/#content

2. Where is the code that starts the download?
B4X:
Sub lblHatsegKovez_Click()
   
    Dim lbl As Label = Sender
    lbl.Enabled = False
   
    Dim cRec As Cursor
    cRec = DBUtils.FnOpenRecSet(Starter.sql,"select * from AttachedFiles WHERE recid = " & lbl.Tag)
    If cRec.RowCount > 0 Then
        cRec.Position = 0
       
        ProgressDialogShow2("Please Wait While Downloading..." ,  True)
        Dim job As HttpJob
        Dim sQuery As String

        job.Initialize("DownLoadFile" , Me)
        sQuery = Starter.QueryAttachedFiles  & Starter.iUserId & "," & DBUtils.Fmsg(Starter.sUserPass) & "," & DBUtils.Fmsg(Starter.sUserTz) & "," & cRec.GetLong("Recordid")
        job.PostString(Starter.ServerUrl , sQuery)
        job.GetRequest.Timeout =DateTime.TicksPerSecond * 300 ' = 10 = 10 Sec
       
    End If   
       
       
       
       

    lbl.Enabled = True
   
   
End Sub



Sub DownloadFile(sFilePath As String,sFileType As String)
   
    Starter.FTP.Initialize("FTP", Starter.FtpServer , 21, Starter.FtpUserName, Starter.FtpPass)
    Starter.FTP.PassiveMode = True
    Starter.FTP.DownloadFile(sFilePath & "." & sFileType,False,Starter.TargetFolder, "tmpFile." & sFileType)
   
   
End Sub
 
Upvote 0

alon

Active Member
Licensed User
Answer the questions

1. You are using which targetsdk in your app (manifest)?
2. You are running the code on a Device running which Android Version?

target 26
Android Version 6.0.1

B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="26"/>
<supports-screens android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" 
    android:anyDensity="true"/>
   
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.CALL_PHONE"/>   
  <uses-permission android:name="android.permission.CAMERA"/>
   
    )
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.DarkTheme)
'End of default text.
 
Upvote 0

alon

Active Member
Licensed User
the code that starts the download is in the Sub JobDone:


B4X:
Sub JobDone (Job As HttpJob)
    If Job.Success Then
        Dim parser As JSONParser
        Dim response As String = Job.GetString
        '        Job.Class_Globals.t
        parser.Initialize(response)
        Dim rows As List
        rows = parser.NextArray
   
        'work with result
        'rows is a List. Each item is a Map with the columns names as keys and the db values as the values.
   
        Select Case Job.JobName
            Case "GetAttachedFiles"
                DBUtils.FnExecuteQuery(Starter.sql,"DELETE FROM AttachedFiles " )
                If rows.Size > 0 Then
                    DBUtils.InsertMaps(Starter.sql,"AttachedFiles",rows)
                    FillAttachedFiles
                End If
               
               
            Case "DownLoadFile"
               
                For i = 0 To rows.Size - 1
                    Log("Rows #" & i)
                    Dim m As Map
                    m = rows.Get(i)
                    '***********  Here I Start The Download *****************    
                    DownloadFile (m.Get("FileName"),m.Get("FileType"))
                    '**************************************************
                    Job.Release
                    Return

                Next
                Job.Release
                Return
               
        End Select
   
    Else
        Select Case Job.JobName
            Case "DownLoadFile"
                Msgbox2("הורדת הקובץ נכשלה!","אחזקה","אישור","","",Null)
       
        End Select
   
    End If
    Job.Release
    ProgressDialogHide
End Sub


Sub DownloadFile(sFilePath As String,sFileType As String)
   
    Starter.FTP.Initialize("FTP", Starter.FtpServer , 21, Starter.FtpUserName, Starter.FtpPass)
    Starter.FTP.PassiveMode = True
    Starter.FTP.DownloadFile(sFilePath & "." & sFileType,False,Starter.TargetFolder, "tmpFile." & sFileType)
   
End Sub


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)
    Starter.FTP.Close
    Log(ServerPath & ", Success=" & Success)
    If Success = False Then
        Log(LastException.Message)
    Else
        Try
           
            Dim sFileType As String 
            sFileType = ServerPath.SubString(ServerPath.Length-4)
            If sFileType.SubString2(0,1) = "." Then
                sFileType = sFileType.SubString(1)
            End If
            Dim i As Intent
            i.Initialize(i.ACTION_VIEW, "file://" & File.Combine(Starter.TargetFolder & "/",  ServerPath))
           
           
            Dim types As Map
            types.Initialize
           
            types.Put("apk","application/vnd.android.package-archive")
            types.Put("au","audio/basic")
            types.Put("avi","video/msvideo, video/avi, video/x-msvideo")
            types.Put("bmp","image/bmp")
            types.Put("bz2","Application/x-bzip2")
            types.Put("css","text/css")
            types.Put("dtd","Application/xml-dtd")
            '            types.Put("doc","Application/msword")
            types.Put("doc","application/msword")
            '            types.Put("docx","Application/vnd.openxmlformats-officedocument.wordprocessingml.document")
            types.Put("docx","application/msword")
            types.Put("dotx","Application/vnd.openxmlformats-officedocument.wordprocessingml.template")
            types.Put("es","Application/ecmascript")
            types.Put("exe","Application/octet-stream")
            types.Put("gif","image/gif")
            types.Put("gz","Application/x-gzip")
            types.Put("hqx","Application/mac-binhex40")
            types.Put("html","text/html")
            types.Put("jar","Application/java-archive")
            types.Put("jpg","image/jpeg")
            types.Put("js","Application/x-javascript")
            types.Put("midi","audio/x-midi")
            types.Put("mp3","audio/mpeg")
            types.Put("mpeg","video/mpeg")
            types.Put("ogg","audio/vorbis, Application/ogg")
            types.Put("pdf","application/pdf")
            types.Put("pl","Application/x-perl")
            types.Put("png","image/png")
            types.Put("potx","Application/vnd.openxmlformats-officedocument.presentationml.template")
            types.Put("ppsx","Application/vnd.openxmlformats-officedocument.presentationml.slideshow")
            types.Put("ppt","Application/vnd.ms-powerpointtd>")
            types.Put("pptx","Application/vnd.openxmlformats-officedocument.presentationml.presentation")
            types.Put("psv","Application/postscript")
            types.Put("qt","video/quicktime")
            types.Put("ra","audio/x-pn-realaudio, audio/vnd.rn-realaudio")
            types.Put("ram","audio/x-pn-realaudio, audio/vnd.rn-realaudio")
            types.Put("rdf","Application/rdf, Application/rdf+xml")
            types.Put("rtf","Application/rtf")
            types.Put("sgml","text/sgml")
            types.Put("sit","Application/x-stuffit")
            types.Put("sldx","Application/vnd.openxmlformats-officedocument.presentationml.slide")
            types.Put("svg","image/svg+xml")
            types.Put("swf","Application/x-shockwave-flash")
            types.Put("tar.gz","Application/x-tar")
            types.Put("tgz","Application/x-tar")
            types.Put("tiff","image/tiff")
            types.Put("tsv","text/TAB-separated-values")
            types.Put("txt","text/plain")
            types.Put("wav","audio/wav, audio/x-wav")
            types.Put("xlam","Application/vnd.ms-excel.addin.macroEnabled.12")
            types.Put("xls","application/vnd.ms-excel")
            types.Put("xlsb","Application/vnd.ms-excel.sheet.binary.macroEnabled.12")
            types.Put("xlsx","application/vnd.ms-excel")
            '            types.Put("xltx","Application/vnd.openxmlformats-officedocument.spreadsheetml.template")
            types.Put("xltx","vnd.ms-excel")
            types.Put("xml","Application/xml")
            types.Put("zip","Application/zip, Application/x-compressed-zip")
           
            If types.ContainsKey(sFileType) Then
                i.SetType(types.Get(sFileType))
            Else
                i.SetType("application/vnd.android.package-archive")
            End If
'           
           
           
           
           
            '
            '
            '            Dim pdfAppsList As List
            '            pdfAppsList = QueryIntent(i)
            '            If pdfAppsList.size>0 Then
            '                ' pdf viewer(s) exists
            '            End If
            '
            '
           
           
            StartActivity(i)
        Catch
            Log(LastException)
        End Try
    End If
End Sub






That is the error .



upload_2018-8-26_10-29-4.png
 
Upvote 0

alon

Active Member
Licensed User
Thanks you all for trying to help me.
I had a problem with the file name that I was trying to open.

Thanks again.....
 
Upvote 0
Top