如何使用Objective-C将存档文件发送和保存到服务器
我在NSDocumentDirectory中有一个存档(.zip)文档的本地路径,希望将其保存在Web服务器上如何使用Objective-C将存档文件发送和保存到服务器,objective-c,file,webserver,save,archive,Objective C,File,Webserver,Save,Archive,我在NSDocumentDirectory中有一个存档(.zip)文档的本地路径,希望将其保存在Web服务器上 NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *archivePath = [documentDirectory stringByAppendingPathCompon
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *archivePath = [documentDirectory stringByAppendingPathComponent:@"CreatedArchive.zip"];
我需要将该文件保存在服务器上,然后从服务器获取.json文档,对其进行更改(写入保存的存档url)并上传回来。我想这是使用ASIFormDataRequest和PHP完成的。也许有人有在服务器上保存存档文件的工作代码?我找到了方法。您可以使用上载任何文件 使用此代码:
// Upload archive
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *archivePath = [documentDirectory stringByAppendingPathComponent:@"CreatedArchive.zip"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://your.url/uploadfile.php"]];
[request setPostValue:@"uploadfilefromapp" forKey:@"action"];
[request setFile:archivePath forKey:@"arch"];
[networkQueue addOperation:request];
[networkQueue go];
uploadfile.php
<?php
if(isset($_POST['action']) && isset($_FILES['arch'])) {
if($_POST['action']==='uploadfilefromapp') {
$newfile = basename($_FILES['arch']['name']);
if(move_uploaded_file($_FILES['arch']['tmp_name'], $newfile)) {
echo 'ok!';
} else {
echo 'fail!';
}
} else {
echo 'Go away cheater';
}
}
?>
它的工作,但如果你知道更好的解决方案,请张贴在这里