How can i write file ( pdf,txt,…) from NotesStream?

大兔子大兔子 提交于 2019-12-24 19:57:52

问题


I have a base64 encoded string. I want to decode it and burn out a file to pass on. I code this:

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

Examples: attachmentValue = "VG9pIGxhIFF1aQ==" and attachmentName = "file.pdf". When I write a txt file, it works fine. But I write a pdf file, it error and can not open. How to fix it? Thanks for help!


回答1:


Set content type to application/pdf for pdf files:

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



回答2:


I find out my mistake. writeText had the wrong format parameter.

Call stream.WriteText(attachmentValue, EOL_CRLF)

works fine.



来源:https://stackoverflow.com/questions/45796962/how-can-i-write-file-pdf-txt-from-notesstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!