iPhone:如何在 XML 中嵌入 NSData 以便上传到服务器?
我想将数据传递到服务器并将文件作为二进制数据存储在数据库中。
NSData *myData = [NSData dataWithContentsOfFile:pathDoc];
pathDoc = [NSString stringWithFormat:@"<size>%d</size><type>%d</type><cdate>%@</cdate><file>%c</file><fname>File</fname>",fileSizeVal,filetype,creationDate,myData];
对此有什么想法吗?
感谢您, 米兰
I want to pass data to a server and store the file there in a database as binary data.
NSData *myData = [NSData dataWithContentsOfFile:pathDoc];
pathDoc = [NSString stringWithFormat:@"<size>%d</size><type>%d</type><cdate>%@</cdate><file>%c</file><fname>File</fname>",fileSizeVal,filetype,creationDate,myData];
Any idea about this?
Thanks you,
Milan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 XML 文档中嵌入二进制数据的最常见方法是将数据编码为 ASCII。例如使用 Base64。
The most common way to embed binary data in an XML document is to encode the data to ASCII. For example using Base64.
这段代码没有任何意义。
您创建一个带有文件路径的 NSData 对象,然后将表示 XML 块的字符串重新分配给该路径 var。非常混乱,很容易导致错误。不要以这种方式重用变量。
无论如何,要将数据编码为字符串,您可以使用 NSString:
然后将该字符串插入到您想要的任何位置。
编辑:
就像这样:(对 Stackoverflow 的编辑器今天的格式设置感到抱歉)
NSUnicodeStringEncoding
是定义各种字符串编码的几个常量之一。请参阅 NSString 类参考。您使用哪一种取决于您的服务器的期望。祝你好运。
This code makes no sense.
You create a NSData object with a path to a file then you turn around and reassign to the path var a string representing a chunk of XML. It's very muddled and likely to cause errors. Don't reuse variables this way.
In any case, to encode data to a string you use NSString's:
Then just insert that string wherever you want it.
Edit:
Like so: (Sorry about the formatting Stackoverflow's editor seems to be on blink today)
The
NSUnicodeStringEncoding
is one of several constants defining a variety of string encodings. See the NSString class reference. Which one you use depends on what your server expects.Good luck.
您应该能够使用对象归档来创建序列化对象,然后将其发送到服务器。
you should be able to use object archiving to create a serialized object and then send that to the server.