上传sqlite文件 [英] Upload an sqlite file

查看:76
本文介绍了上传sqlite文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这个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";
    }
    ?>

打印:


错误域= com.alamofire.error.serialization.response代码= -1016
请求失败:不可接受的内容类型:text / html
UserInfo = 0x7b8b2bf0

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

问题来自 mimeType ?我还在iOS和php端使用了 application / x-sqlite3 内容类型。

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

推荐答案

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

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.

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

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:


错误域= com.alamofire.error.serialization.response代码= -1016请求失败:不可接受的内容类型:text / htmlUserInfo = 0x7b8b2bf0

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

这通常是服务器页面返回的结果 text / html ,但 AFURLSessionManager 期待JSON。

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

您可以更改经理的 responseSerializer

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

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

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

这篇关于上传sqlite文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆