-3
我真的很累,在這裏搜索和谷歌,但壞的結果。POST SQLITE文件到遠程服務器
要問,如果我想在文件SQLITE文件/本地上傳到我的服務器
我怎麼能做到這一點從iOS的做一些備份數據庫的
你的回答將得到高度讚賞。
我真的很累,在這裏搜索和谷歌,但壞的結果。POST SQLITE文件到遠程服務器
要問,如果我想在文件SQLITE文件/本地上傳到我的服務器
我怎麼能做到這一點從iOS的做一些備份數據庫的
你的回答將得到高度讚賞。
對於HTTP正文中的文件,標頭Content-Type
和Content-Type: application/octet-stream
使用multipart/form-data
。
OK,我會告訴UA演示,剛剛創建的模板 '空白應用程序' 的新Xcode項目,選中 '使用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));
}
我會嘗試並很快回復..提前致謝 – SimpleojbC
所以你問如何將任意文件的內容作爲HTTP有效載荷傳輸? – SLaks
@SLaks我認爲所以我需要將name.sqlite發佈到我的服務器,其中name.sqlite在iDevice的文檔路徑 – SimpleojbC
有很多關於如何打開NSURLConnection和POST數據到服務器的stackoverflow和互聯網上的信息。 –