Upload an sqlite file

元气小坏坏 提交于 2019-12-06 06:58:46

问题


I'm using AFNetworking to try to upload a file:

-(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];
}

And this php file 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";
    }
    ?>

It's printing:

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

Is the problem from the mimeType ? I also used application/x-sqlite3 content type in both iOS and php sides.


回答1:


The client-side code is uploading using the file field name, but the server code is looking for uploadedfile. You have to use the same field name on both platforms.

The mime type should be fixed. Because SQLite files are binary files, not text files, I'd suggest a mime type of application/x-sqlite3 or application/octet-stream, rather than text/html or text/plain. But the mismatched field name is the more egregious issue.


By the way, you report an error message:

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

This is generally a result that your server page is returning text/html, but AFURLSessionManager is expecting JSON.

You can change the manager's responseSerializer:

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

Or, better, change your server code to return JSON.



来源:https://stackoverflow.com/questions/27638318/upload-an-sqlite-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!