Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm really got tired, searching here and in google but bad result.

Am asking about if I want to upload SQLITE file in documents/local to my server

how could I do that from iOS to do some of backup database

Your answer will be highly appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
425 views
Welcome To Ask or Share your Answers For Others

1 Answer

Use multipart/form-data for header Content-Type, and Content-Type: application/octet-stream for a file in HTTP body.

OK, I will show u a demo, just create an new Xcode project with template 'Empty Application', check 'Use ARC', then paste :

#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:@"--%@
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [postData appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data; name="file"; filename="test.sqlite"
"] dataUsingEncoding:NSUTF8StringEncoding]];
        [postData appendData:[@"Content-Type: application/octet-stream

" dataUsingEncoding:NSUTF8StringEncoding]];
        [postData appendData:fileData];
        [postData appendData:[[NSString stringWithFormat:@"
--%@--
", 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

And the server side, 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 ) );
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...