1

I'm writing a Python program to build s an XML file that will be processed by Biztalk to import data into another system.

One of the fields allows me to include a file in a base64binary field. I'm doing so using base64.b64encode(data):

import base64
data = open('Test.pdf', 'rb').read()
print base64.b64encode(data)

However the expected data must begin with a 0x.

Looking into a sample XML file I find that the example encoded data looks like hexadecimal (no symbols and no letters above F) so I also tried that with no luck:

import binascii
print '0x' + binascii.hexlify(data)

How can you use Python to properly encode a file to insert it into a base64binary XML field?

Daniel Reis
  • 12,944
  • 6
  • 43
  • 71
  • 1
    Judging from this http://www.schemacentral.com/sc/xsd/t-xsd_base64Binary.html standard base64 should be fine. Wonder why it's not in this particular case. – pajton Oct 17 '13 at 12:05
  • See this http://stackoverflow.com/questions/208894/how-to-base64-encode-a-pdf-file-in-python/208960#208960 – Dijkgraaf Oct 17 '13 at 19:54

1 Answers1

2

It turns out that base64, as outlined in my question's code, is correct. There was a misinterpretation of the error logs.

So if anyone reading this has a similar problem, let me assure you: base64 is fine, the problem must be something else.

Daniel Reis
  • 12,944
  • 6
  • 43
  • 71
  • In my case, I was trying to encode PDF files to Base64 too. and it worked perfectly. You've saved me a alot of time! – KareemJ May 20 '19 at 07:13