AFNetworking 2.0 multipart/form-data 上传到 mySQL
AFNetworking 2.0 multipart/form-data upload to mySQL
我尝试了很多方法,但似乎无法正常工作。我可以通过 UITextField 将文本上传到 mySQL 数据库,我可以通过 UIImageView 分别上传图像,但是当我尝试将它们放在一起时它不起作用:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",nil];
NSDictionary *parameters = @{@"new": self.enter};
NSData *imageData = UIImageJPEGRepresentation(self.uploadImage.image, 0.5);
[manager POST:@"http://emily-ip.com/write.php" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"image" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
此代码给出错误:
"The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x7fabeb081660
所以我把这个放在:
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
这会上传图片但不会上传从文本字段输入的文本,这是我得到的输出:
Success: <4572726f 72>
Success: <53756363 65737366 756c>
看了那么多帖子也没找到问题所在。无论如何,我认为它与文本字段有关。有人可以帮我修复代码吗?
我想通了。这是 post 来自文本字段和 UIImageVIew 的图像和文本:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",nil];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSDictionary *parameters = @{@"new": self.enter.text};
NSData *imageData = UIImageJPEGRepresentation(self.uploadImage.image, 0.5);
[manager POST:@"http://emily-ip.com/write.php" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"image" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
我尝试了很多方法,但似乎无法正常工作。我可以通过 UITextField 将文本上传到 mySQL 数据库,我可以通过 UIImageView 分别上传图像,但是当我尝试将它们放在一起时它不起作用:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",nil];
NSDictionary *parameters = @{@"new": self.enter};
NSData *imageData = UIImageJPEGRepresentation(self.uploadImage.image, 0.5);
[manager POST:@"http://emily-ip.com/write.php" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"image" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
此代码给出错误:
"The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x7fabeb081660
所以我把这个放在:
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
这会上传图片但不会上传从文本字段输入的文本,这是我得到的输出:
Success: <4572726f 72>
Success: <53756363 65737366 756c>
看了那么多帖子也没找到问题所在。无论如何,我认为它与文本字段有关。有人可以帮我修复代码吗?
我想通了。这是 post 来自文本字段和 UIImageVIew 的图像和文本:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",nil];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSDictionary *parameters = @{@"new": self.enter.text};
NSData *imageData = UIImageJPEGRepresentation(self.uploadImage.image, 0.5);
[manager POST:@"http://emily-ip.com/write.php" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"image" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];