B4J Question error attachment with file excel XLSX

juvanum

Active Member
Licensed User
Longtime User
Hello,
I'm using the erel' solution " Building a mini Email based server" to receive a file excel.

if the file attached is XLS, everything is ok.
But if the file attached is XLSX the file is not saved correctly (see image)
screen.png


maybe the problem is here but I can't find ...
any suggestions?
thank you


B4X:
Sub HandlePart(Headers As String, Body As String, Msg As Message)
    If Regex.Matcher2("Content-Transfer-Encoding:\s*base64", _
        Regex.CASE_INSENSITIVE, Headers).Find Then
        'we are dealing with an attachment
        Dim filename As String
        Dim m As Matcher
        m = Regex.Matcher2("filename=\s*q([^q]+)q".Replace("q", QUOTE), Regex.CASE_INSENSITIVE, Headers)
        If m.Find Then filename = m.Group(1) Else filename = "attachment" & (Msg.Attachments.Size + 1)
        Dim su As StringUtils
        Dim out As OutputStream
        out = File.OpenOutput(dir, filename, False)
        Dim data() As Byte
        data = su.DecodeBase64(Body)
        Log("file saved: "  & filename & " (" & data.Length & " bytes)")
        out.WriteBytes(data, 0, data.Length)
        out.Close
        Msg.Attachments.Add(filename)
    Else If Regex.Matcher2("Content-Type:\s*text/", _
        Regex.CASE_INSENSITIVE, Headers).Find Then
        Msg.Body = Body
    End If
End Sub
 

juvanum

Active Member
Licensed User
Longtime User
1. Compare the received file content and the original file content.
the file received is less than 1 byte. (see image)
length file sent = 9727 bytes
length file received = 9726 bytes


2. Try to zip the file before you send it.
it works but I can't offer this solution to my customer...

compara.png
 
Upvote 0

juvanum

Active Member
Licensed User
Longtime User
more info:
this is the file sent (unzipped)
 

Attachments

  • provatel.zip
    7 KB · Views: 147
Upvote 0

juvanum

Active Member
Licensed User
Longtime User
You need to analyze the raw email and see where does the byte get lost.

the error is in the last position.

this is the code that I have used to test (see image)

B4X:
Sub FileCompare
    Dim SentDir As String ="C:\Users\admin\Desktop"
    Dim ReceivedDir As String="C:\democapcost1\Fornitori Allegati IN\Sconosciuti"

    Compare(Bit.InputStreamToBytes(File.OpenInput(SentDir,"provatel.xlsx")), _
     Bit.InputStreamToBytes(File.OpenInput(ReceivedDir,"provatel.xlsx")))
End Sub

Sub Compare(array1() As Byte, array2() As Byte) 
    Log ("lenght file sent "& array1.Length )
    Log ("lenght file received "& array2.Length )

    For i = 0 To array2.Length - 1
        If array1(i) <> array2(i) Then
            Log ("error in position: " &i)
            Return 
        End If
    Next
    If array1.Length <> array2.Length Then
        Log ("error in last position" )
    Else
        Log ("file ok" )               
    End If
    Return 
End Sub

2019-05-05.png
 
Upvote 0

juvanum

Active Member
Licensed User
Longtime User
Please post the logs as text. As I wrote you will need to debug the MailParser code and see what causes one byte to be lost.
Sorry Erel,
maybe I explained myself badly in the previous post ... But if the file received is egual as the file sent except for the last character that is missing, it is obvious that the code, for some reason, does not copy the last character.
 
Upvote 0
Top