-(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
$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.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。