2017-08-21 2 views
0

J'ai une chaîne encodée en base64. Je veux le décoder et brûler un fichier à transmettre. Je code ceci:Comment puis-je écrire un fichier (pdf, txt, ...) à partir de NotesStream?

Function DecodeBase64(attachmentValue As String,attachmentName As String) As String 
    Dim result As String 
    Dim s As New NotesSession 
    Dim fileOut As String 
    Dim fout As Integer 
    Dim foutOpen As Integer 
    Dim stream As NotesStream 
    Dim dc As NotesDocumentCollection 

    'fout = Freefile 

    fileOut = "C:\ExportFileLotus\" + attachmentName 
    'Open fileOut For Output As fout 
    'foutOpen = True 
    Set db = s.CurrentDatabase 
    Set doc = db.CreateDocument 

    s.ConvertMIME = False ' Do not convert MIME to rich text 
    Set body = doc.CreateMIMEEntity 
    Set header = body.CreateHeader(attachmentName) 
    Call header.SetHeaderVal("MIME document") 
    Set stream = s.CreateStream 
    Call stream.WriteText(attachmentValue,EOL_NONE) 
    If stream.Bytes = 0 Then 
     Messagebox fileInput,,"File has no content" 
     Goto ExitSub 
    End If 
    'Call body.SetContentFromText(stream, "text/plain;charset=UTF-8", ENC_BASE64) 
    'Call body.SetContentFromText(stream, "charset=UTF-8", ENC_BASE64) 
    Call body.SetContentFromBytes(stream, "", ENC_BASE64) 
    Call body.DecodeContent() 
    stream.Open fileOut, "binary" 
    body.GetContentAsBytes stream, True 
    stream.Close 

    'Print #fout, body.ContentAsText 
    'Close #fout 
    DecodeBase64 = fileOut 
ExitSub: 
    s.ConvertMIME = True ' Restore conversion 
End Function 

Exemples: attachmentValue = "VG9pIGxhIFF1aQ ==" et ATTACHMENTNAME = "fichier.pdf". Lorsque j'écris un fichier txt, cela fonctionne très bien. Mais j'écris un fichier pdf, il erreur et ne peut pas ouvrir. Comment le corriger? Merci pour l'aide!

+0

Pour combien de temps cette fonction est-elle disponible? Appelez stream.WriteText (attachmentValue, EOL_CRLF) .Elle fonctionne correctement –

Répondre

1

Set type de contenu à application/pdf pour les fichiers pdf:

Call body.SetContentFromText(stream, "application/pdf", ENC_BASE64) 
+0

Comment définir le type de contenu à l'application/pdf pour pdf? –

+0

J'ai prolongé ma réponse. –

+0

C'est encore buggé. Quand j'ouvre le fichier pdf, il informe: "Erreur de format.Pas un PDF ou coruppted" –

1

Je trouve mon erreur. writeText avait le mauvais paramètre de format.

Call stream.WriteText(attachmentValue, EOL_CRLF) 

fonctionne très bien.