androniennn
androniennn

Reputation: 3137

Upload an sqlite file

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.

Upvotes: 4

Views: 1291

Answers (1)

Rob
Rob

Reputation: 437672

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.

Upvotes: 6

Related Questions