OGeek|极客世界-中国程序员成长平台

标题: iPhone PLIST 文件上传二进制文件 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 07:00
标题: iPhone PLIST 文件上传二进制文件

我有一个内置在 Objective C 中的 iPhone 应用程序,它可以将 PLIST 文件从 iPhone 上传到服务器。大多数时候它工作得很好,但有时文件看起来像这样

bplist00Ú .....

我发现这显然是一个二进制 PLIST,但我不知道为什么会发生这种情况。

有什么想法吗?

NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent"Documents"] stringByAppendingPathComponent:fileName];
NSData *data = [[NSData alloc] initWithContentsOfFile:path]; 

// Location ID doesn't seem to be correct here - always 0?
NSMutableString *urlString = [NSMutableString string];
[urlString appendString:[NSString stringWithFormat"http://www.website.com/app/iphone/submitjob/upload?jid=%i&mid=%@&lid=%i", delegate.jobInProgress.jobID, delegate.memberID, [[delegate.jobInProgress.plist objectForKey"locationID"] intValue]]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod"OST"];

NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat"multipart/form-data; boundary=%@", boundary];

[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", fileName] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:data]];
[postbody appendData:[[NSString stringWithFormat"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
returnString = [returnString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];



Best Answer-推荐答案


您要上传 bundle 中的现有 .plist 吗?因为项目中的所有 .plist 都转换为二进制格式。我会使用 CFPropertyListWrite 创建您从头开始上传到服务器的任何 .plist并将格式设置为 kCFPropertyListXMLFormat_v1_0。然后将生成的文件上传到您的服务器。这样一来,您就不会依赖包中的格式。

关于iPhone PLIST 文件上传二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5412337/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4