从iPhone上传plist到远程服务器

时间:2011-08-17 06:00:45

标签: iphone xcode upload plist dropbox

是否可以将整个plist文件从iPhone上以编程方式上传到远程服务器或Dropbox?

任何有用的信息都会有所帮助。如果你有代码请分享谢谢! :)

编辑:

我正在使用这些代码,我是从Upload File to FTP Server on iPhone获得的。但是我有错误,我的plist在文档目录中。

功能

- (BOOL) uploadData:(NSData *)plistData filename:(NSString *)filename {
NSString *urlString = @"http://site.net/upload.php";

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

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

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

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"Uploaded: %@", returnString);
return ([returnString isEqualToString:@"OK"]);

}

通话方法

[self uploadData:[NSKeyedArchiver archivedDataWithRootObject:dbArray] filename:@"data.plist"];

PHP文件

    <?php
$uploaddir = '';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "OK";
} else {
    echo "ERROR";
}

?>

它只是不断抛出“错误”......

1 个答案:

答案 0 :(得分:0)

对于通用服务器,您只需编写将接受POST请求的脚本(例如使用PHP)。然后,您可以使用NSURLConnection上传文件。

像往常一样,official documentation已经涵盖了你。至于服务器端脚本,这取决于您选择的语言/工具包。 PHP的move_uploaded_file()只是其中一种可能性。