-3

我真的很累,在这里和谷歌搜索,但结果不好。

我在问我是否想将文档/本地中的 SQLITE 文件上传到我的服务器

我怎么能从 iOS 做一些备份数据库

您的回答将不胜感激。

4

1 回答 1

0

用于multipart/form-dataheaderContent-TypeContent-Type: application/octet-streamHTTP 正文中的文件。

好的,我将展示 ua 演示,只需使用模板“Empty Application”创建一个新的 Xcode 项目,选中“Use 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 ) );
}
于 2013-07-16T12:29:44.693 回答