简体   繁体   English

两个NSData对象-base64编码为1个“包”

[英]Two NSData objects - base64 encode as 1 “package”

I have 2 NSData objects (a video + thumbnail) that I want to send over the wire. 我有2个要通过电线发送的NSData对象(视频+缩略图)。 I want to base64 encode both the objects as a package in order to chunk them and send over. 我想将两个对象都作为一个包进行base64编码,以便对它们进行分块并发送。

The receiver, after receiving all the chunks can then unpackage and have the 2 objects. 接收器在收到所有块之后可以解包并拥有2个对象。

How can I combine the 2 NSData objects into a single base64 string such that it can easily be unpackaged on the receiver side? 如何将2个NSData对象合并为单个base64字符串,以便可以轻松在接收方将其拆包?

You have several options. 您有几种选择。

  1. You could zip the two files together and get the NSData of the zipped file. 您可以将两个文件压缩在一起,并获得压缩文件的NSData 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. 然后,每个文件后跟4个字节。 Each of the 4 bytes would represent the length of the data for each file. 4个字节中的每个字节代表每个文件的数据长度。 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. 因此,您的数据将变成1字节计数,4字节系列,然后是每个文件的字节,从而构成一个大NSData ,然后对它进行base64编码。 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. 您可以执行一个HTTP POST,其中包含每个文件名和每个文件。 Search on "http post files multipart/form-data". 搜索“ http帖子文件multipart / form-data”。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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