2013-07-15 7 views
-3

나는 여기에서 그리고 Google에서 나쁜 결과를 검색, 정말 피곤 해요.원격 서버에 POST SQLITE 파일

암은 내가 아이폰 OS에서 당신의 대답은 높게 평가 될 것이다

백업 데이터베이스의 일부를 수행하는 것을 할 수있는 방법을 내 서버

에 로컬 문서에서 SQLite는 파일을/업로드하려면에 대해 물어.

+0

그래서 임의 파일의 내용을 HTTP 페이로드로 전송하는 방법을 묻는 중입니까? – SLaks

+0

@SLaks 나는 내 서버에 name.sqlite 문서 경로에있는 iDevice – SimpleojbC

+0

을 게시해야 할 필요가 있다고 생각합니다. stackoverflow 및 인터넷에 대한 정보가 NSURLConnection 및 POST 데이터를 서버에 여는 방법에 대한 정보가 많이 있습니다. . –

답변

0

헤더 Content-Type에 대한 사용 multipart/form-data 및 HTTP 본문의 파일에 대한 Content-Type: application/octet-stream.

OK, I 붙여 다음, '사용 ARC'를 확인, 단지 템플릿 '빈 응용 프로그램'과 새로운 Xcode 프로젝트를 생성, UA 데모를 표시합니다 :

#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

관련 문제