当前位置:首页 > 数据库 > SQlite

php – 上传sqlite文件

我正在使用AFNetworking尝试上传文件:

-(void)uploadFile{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"data.sqlite"];
    NSURL *filePathURL = [NSURL fileURLWithPath:filePath];

    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://localhost:8888/myApp/upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileURL:filePathURL name:@"file" fileName:@"data.sqlite" mimeType:@"text/html" error:nil];
    } error:nil];

    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSProgress *progress = nil;

    NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"Error: %@", error);
        } else {
            NSLog(@"Success: %@ %@", response, responseObject);
        }
    }];

    [uploadTask resume];
}

而这个php文件upload.php:

<?php

    $uploaddir = 'uploads/';
    $file = basename($_FILES['uploadedfile']['name']);
    $uploadfile = $uploaddir . $file;

    echo "file=".$file;

    if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $uploadfile)) {
        echo $file;
    }
    else {
        echo "error";
    }
    ?>

这是打印:

Error Domain=com.alamofire.error.serialization.response Code=-1016
“Request failed: unacceptable content-type: text/html”
UserInfo=0x7b8b2bf0

是mimeType的问题吗?我还在iOS和php方面使用了application / x-sqlite3内容类型.

解决方法:

客户端代码使用文件字段名称上载,但服务器代码正在查找uploadfile.您必须在两个平台上使用相同的字段名称.

mime类型应该是固定的.因为SQLite文件是二进制文件,而不是文本文件,我建议使用mime类型的application / x-sqlite3或application / octet-stream,而不是text / html或text / plain.但不匹配的字段名称是更令人震惊的问题.

顺便提一下,您报告错误消息:

Error Domain=com.alamofire.error.serialization.response Code=-1016 “Request failed: unacceptable content-type: text/html” UserInfo=0x7b8b2bf0

这通常是因为您的服务器页面返回text / html,但AFURLSessionManager期待JSON.

您可以更改经理的responseSerializer:

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

或者,更好的是,更改您的服务器代码以返回JSON.


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!