SQLITE数据库上传

时间:2018-05-26 08:33:50

标签: ios objective-c iphone sqlite

有没有办法上传,下载到服务器从iPhone应用程序中读取文档目录中的.sqlite文件,并将其用作数据库。 我尝试转换为二进制数据并上传,但文件正在加密我从服务器恢复/下载后无法读取或写入。 有什么建议吗?

上传任务获取文件

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *chatDatabasePath = [documentsDirectory stringByAppendingPathComponent:@"ChatDB.sqlite"];
NSString *contactDatabasePath = [documentsDirectory stringByAppendingPathComponent:@"ContactDB.sqlite"];
NSURL*ChatDB_url = [NSURL fileURLWithPath:chatDatabasePath];
NSURL*ContactDB_url = [NSURL fileURLWithPath:contactDatabasePath];
self.ChatDBData = [NSData dataWithContentsOfURL:ChatDB_url];
self.ContactDBData = [NSData dataWithContentsOfURL:ContactDB_url];

上传

-(void)UploadDbToServer:(NSData*)DB_Data ForDbName:(NSString*)dbName forExtension:(NSString*)extension andCallback:(void (^)(id))callback{

NSString *urlString = [[NSString alloc]initWithString:[NSString stringWithFormat:@"%@chat-backup-upload",baseUrl]];
urlString=[urlString stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:100];
[request setHTTPMethod:@"POST"];

NSString *boundary = @"7MA4YWxkTrZu0gW";
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];

// post body
NSMutableData *body = [NSMutableData data];

// add DB data
if (DB_Data) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"DB\"; filename=\"%@.%@\"\r\n",dbName,extension] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: file/sqlite\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:DB_Data];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// setting the body of the post to the reqeust
[request setHTTPBody:body];


NSURLSessionConfiguration *sessionConf = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConf];

NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

    id jsonResponse= [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
    callback ((id) jsonResponse);
}];

[task resume];}

正在下载

(void)startDownload:(NSURL*)url {
NSURL *Url= [NSURL URLWithString:[NSString stringWithFormat:@"%@",url]];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[queue setMaxConcurrentOperationCount:5];

self.activeUrlString=[NSString stringWithFormat:@"%@",Url];
if (!self.session) {
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"image"];
    self.session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:queue];
}

NSURLRequest* request = [NSURLRequest requestWithURL:Url];

self.task = [self.session downloadTaskWithRequest:request];

[self.task resume];}

编写下载的文件

-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {

//saving the files
NSData *DBData = [NSData dataWithContentsOfURL:location];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *chatDatabasePath = [documentsDirectory stringByAppendingPathComponent:@"ChatDB.sqlite"];
NSString *contactDatabasePath = [documentsDirectory stringByAppendingPathComponent:@"ContactDB.sqlite"];

if ([self.activeDownload isEqualToString:@"chat"]){

    [DBData writeToFile:chatDatabasePath atomically:YES];
    self.activeDownload = @"contact";
    [self startDownload:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",pingovaURL,self.ContactDB_URL]]];
}else{
    [DBData writeToFile:contactDatabasePath atomically:YES];   }}

0 个答案:

没有答案