2013-07-15 65 views
-3

我真的很累,在这里搜索和谷歌,但坏的结果。POST SQLITE文件到远程服务器

要问,如果我想在文件SQLITE文件/本地上传到我的服务器

我怎么能做到这一点从iOS的做一些备份数据库的

你的回答将得到高度赞赏。

+0

所以你问如何将任意文件的内容作为HTTP有效载荷传输? – SLaks

+0

@SLaks我认为所以我需要将name.sqlite发布到我的服务器,其中name.sqlite在iDevice的文档路径 – SimpleojbC

+0

有很多关于如何打开NSURLConnection和POST数据到服务器的stackoverflow和互联网上的信息。 –

回答

0

对于HTTP正文中的文件,标头Content-TypeContent-Type: application/octet-stream使用multipart/form-data

OK,我会告诉UA演示,刚刚创建的模板 '空白应用程序' 的新Xcode项目,选中 '使用ARC',然后粘贴:

#import "AppDelegate.h" 

@interface AppDelegate() 
@property (nonatomic, strong) NSURLConnection *urlConnection; 
@property (nonatomic, strong) NSMutableData *receivedData; 
@end 

@implementation AppDelegate 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
     [self.window makeKeyAndVisible]; 
     NSString *localFile = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) 
           objectAtIndex:0] stringByAppendingPathComponent:@"user.sqlite"]; 
     NSString *api = @"http://192.168.0.170/test/upload/upload.php"; 
     [self sendFile:localFile toServer:api]; 
     return YES; 
} 

- (void)sendFile:(NSString *)filePath toServer:(NSString *)serverURL 
{ 
     NSData *fileData = [NSData dataWithContentsOfFile:filePath]; 
     if (!fileData) { 
       NSLog(@"Error: file error"); 
       return; 
     } 

     if (self.urlConnection) { 
       [self.urlConnection cancel]; 
       self.urlConnection = nil; 
     } 

     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] 
             initWithURL:[NSURL URLWithString:serverURL]]; 
     [request setTimeoutInterval:30.0]; 
     [request setHTTPMethod:@"POST"]; 
     NSString *boundary = @"780808070779786865757"; 

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

     /* Body */ 
     NSMutableData *postData = [NSMutableData data]; 
     [postData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [postData appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data; name=\"file\"; filename=\"test.sqlite\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [postData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
     [postData appendData:fileData]; 
     [postData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [request setHTTPBody:postData]; 

     self.urlConnection = [NSURLConnection connectionWithRequest:request delegate:self]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
     if (self.receivedData) { 
       self.receivedData = nil; 
     } 
     self.receivedData = [[NSMutableData alloc] init]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
     [self.receivedData appendData:data]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
     NSLog(@"finish requesting: %@", [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding]); 
     self.urlConnection = nil; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
     NSLog(@"requesting error: %@", [error localizedDescription]); 
     self.urlConnection = nil; 
} 

@end 

而在服务器端,PHP:

<?php 

$uploaddir = './uploads/'; 

if(!file_exists($uploaddir)) @mkdir($uploaddir); 
$file = basename($_FILES['file']['name']); 
$uploadfilename = rand() . '-' . $file; 
$uploadfile = $uploaddir . $uploadfilename; 
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) { 
     $fileURL = "http://192.168.0.170/test/upload/uploads/{$uploadfilename}"; 
     // echo '<a href=' . $fileURL . '>' . $fileURL . '</a>'; 
     $jsonArray = array( 
       'status' => 1, 
       'url' => $fileURL, 
     ); 
     echo json_encode($jsonArray); 
} else { 
     echo json_encode(array('status' => -1)); 
} 
+0

我会尝试并很快回复..提前致谢 – SimpleojbC