【问题标题】:POST SQLITE file to remote serverPOST SQLITE 文件到远程服务器
【发布时间】:2013-07-13 19:37:45
【问题描述】:

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

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

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

我们将非常感谢您的回答。

【问题讨论】:

  • 那么您是在问如何将任意文件的内容作为 HTTP 有效负载传输?
  • @SLaks 我想所以我需要将 name.sqlite 发布到我的服务器,其中 name.sqlite 在 iDevice 的文档路径中
  • 在 stackoverflow 和互联网上有很多关于如何打开 NSURLConnection 和 POST 数据到服务器的信息。
  • @NicholasHart 所有关于 IMAGE 的示例 .. SQLITE 没有示例,请您帮帮我
  • 您可以将任何数据发布到您的服务器,无论是字符串、图像、任何类型的文件,包括原因的sqlite。只是谷歌如何在 iOS 上发送 POST。请参阅example。此外,您可以使用 NSURLConnection、AFNetworking、ASIHTTPRequest 或其他网络框架。

标签: ios sqlite upload


【解决方案1】:

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

好的,我给你看一个演示,用模板'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 ) );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    • 2019-01-28
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多