问题
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