Transfer binary data (image etc) in a CSV file

前端 未结 2 986
误落风尘
误落风尘 2021-01-13 22:55

I have some binary data to transfer in addition to some other metadata information stored as string, number etc. The binary data I have is image files stored as a blob colum

2条回答
  •  再見小時候
    2021-01-13 23:37

    Base64

    The usual (and proper) way is to encode the binary data in Base64. This will make the data larger by a factor 4:3.

    While CSV files are generally considered to be text files, you could write raw binary data to the file.

    That data should then be enclosed in duoble quotes, and all existing double quotes inside the data have to be escaped with another double quote. Quoting the fields will also take care of any newlines in the binary data, but the reader has to support that. There could also be null bytes inside which should not be a problem if the reader knows it's reading binary data (i.e. if you provide the reader yourself).

    Problems could arise however if your data has to be in some unicode form..

    So, in general it is not good practice to write raw binary data to a csv file, it's better to use base64 encoding.

提交回复
热议问题