简体   繁体   中英

Two NSData objects - base64 encode as 1 “package”

I have 2 NSData objects (a video + thumbnail) that I want to send over the wire. I want to base64 encode both the objects as a package in order to chunk them and send over.

The receiver, after receiving all the chunks can then unpackage and have the 2 objects.

How can I combine the 2 NSData objects into a single base64 string such that it can easily be unpackaged on the receiver side?

You have several options.

  1. You could zip the two files together and get the NSData of the zipped file. Then the server could unzip the resulting data file.

  2. You could precede the two chunks of data with a simple set of bytes telling the server about what is coming. The data could be one byte representing the number of files. Then that would be followed by 4 bytes for each file. Each of the 4 bytes would represent the length of the data for each file. So your data becomes the 1 byte count, the series of 4 bytes, then the bytes for each file making one big NSData that you then base64 encode. The server would then decode the data, look at the 1st byte to know the file count, then read the set of file lengths. Then based on each length, it would know what section of data belongs to each file.

  3. You could do an HTTP POST that includes each filename and each file. Search on "http post files multipart/form-data".

一种真正简单的方法是对Base64分别编码每个文件,并将生成的Base64字符串与一个永远不会出现在Base64数据中的字符(例如逗号)连接起来。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM