Utf-8-sig and Blobmedia-objects

Hi,

I have an app that has the option to export a .txt-file with some data that can be imported to another application. This file needs the utf-8 BOM marker applied so the æ, ø and å-characters in the norwegian alphabet get properly imported into the other application.
It’s not a problem to use the ‘utf-8-sig’-entry in python, so is there a way to solve this in Anvil?

Current error:
LookupError: unknown encoding: utf-8-sig

code:
file_content = anvil.BlobMedia(“text/plain”, output_string.encode(‘utf-8-sig’), name=“questions.txt”)

Is this client or server side code?

This is on the client-side. Moved the logic server-side, and it seems to work properly now. :slight_smile:

Then that is the problem: you Mr one of the limitation of the client side python.

The rule of thumb is that, if you need a round trip, you should do as much as you can on the server side and return the final product to the client. Then the client should take care of the ui and as little logic as possible.

If you want it to happen on the client side you can also add the BOM yourself

utf8_bom = b"\xef\xbb\xbf"
content = output_string.encode("utf-8")
file_content = anvil.BlobMedia("text/plain", utf8_bom + content, name="questions.text")

Only utf-8 and ascii encoding are supported on the client.

The alternative is to do this on the server as @stefano.menci suggests.

1 Like

Thank you, stucork, It’s nice to know it is actually possible on the client side.

But as I already mentioned, I moved it server-side so it’s not an issue anymore.
I also changed it to export the data in Excel-format as I read adding BOM-markers should be avoided due to compatibility-issues.