iPhone:如何在 XML 中嵌入 NSData 以便上传到服务器?

发布于 2024-08-25 14:21:14 字数 381 浏览 6 评论 0原文

我想将数据传递到服务器并将文件作为二进制数据存储在数据库中。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

李白 2024-09-01 14:21:14

在 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.

醉梦枕江山 2024-09-01 14:21:14

这段代码没有任何意义。

您创建一个带有文件路径的 NSData 对象,然后将表示 XML 块的字符串重新分配给该路径 var。非常混乱,很容易导致错误。不要以这种方式重用变量。

无论如何,要将数据编码为字符串,您可以使用 NSString:

- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding

然后将该字符串插入到您想要的任何位置。

编辑:

您好,抱歉我不知道该怎么做
使用这个 - (id)initWithData:(NSData
*)数据编码:(NSStringEncoding)中的编码
我的代码。

就像这样:(对 Stackoverflow 的编辑器今天的格式设置感到抱歉)

NSData *myData = [NSData dataWithContentsOfFile:pathDoc]; 
NSString *myDataString = [[NSString alloc] initWithData:myData encoding:NSUnicodeStringEncoding];
NSString *formatString = @"<size>%d</size><type>%d</type><cdate>%@</cdate><file>%c</file><fname>File</fname><data>%@</data>";
pathDoc = [NSString stringWithFormat:formatString,fileSizeVal,filetype,creationDate,myDataString];

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:

- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding

Then just insert that string wherever you want it.

Edit:

Hello, sorry for i dont know how to
use this - (id)initWithData:(NSData
*)data encoding:(NSStringEncoding)encoding in
my code.

Like so: (Sorry about the formatting Stackoverflow's editor seems to be on blink today)

NSData *myData = [NSData dataWithContentsOfFile:pathDoc]; 
NSString *myDataString = [[NSString alloc] initWithData:myData encoding:NSUnicodeStringEncoding];
NSString *formatString = @"<size>%d</size><type>%d</type><cdate>%@</cdate><file>%c</file><fname>File</fname><data>%@</data>";
pathDoc = [NSString stringWithFormat:formatString,fileSizeVal,filetype,creationDate,myDataString];

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.

反话 2024-09-01 14:21:14

您应该能够使用对象归档来创建序列化对象,然后将其发送到服务器。

you should be able to use object archiving to create a serialized object and then send that to the server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文