Download sqlite file

I use AFNetworking to download 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";
    }
    ?>

Print:

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

Is the problem with mimeType? I also used the content type application/x-sqlite3on both the iOS side and php.

+4
source share
1 answer

Client code is loaded using the field name file, but the server code is searched uploadedfile. You must use the same field name on both platforms.

mime . SQLite , , mime application/x-sqlite3 application/octet-stream, text/html text/plain. .


, :

= com.alamofire.error.serialization.response = -1016 " : : /html " UserInfo = 0x7b8b2bf0

, text/html, AFURLSessionManager JSON.

responseSerializer:

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

, , , JSON.

+5

Source: https://habr.com/ru/post/1568893/


All Articles