我正用这个把头撞在墙上。我想UIImage
从库中选择并将其上传到服务器,就像 on 可以用<form action="http://blabla.request.cfm" method="post" enctype="multipart/form-data">
. 我得到了这个错误,而不是成功:
error = Error Domain=NSCocoaErrorDomain Code=3840 “操作无法完成。(Cocoa 错误 3840。)”(JSON 文本没有以数组或对象开头,并且允许未设置片段的选项。) UserInfo=0x145e5d90 {NSDebugDescription= JSON 文本未以数组或对象开头,并且允许未设置片段的选项。}
我试过这样:
-(void)uploadPhoto{
NSString *path = @"http://blabla.request.cfm";
NSData *imageData = UIImageJPEGRepresentation(self.imageView.image, 0.9);
int priv = self.isPrivate ? 1 : 0;
NSDictionary *parameters = @{@"username": self.username, @"password" : self.password, @"private" : @(priv), @"photo" : imageData};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:path parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if(self.imageView.image){
[formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"[UploadVC] success = %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"[UploadVC] error = %@", error);
}];
[self blockView:self.view block:YES];
}
但它不工作......服务器说没有文件。不确定加密是错误的、mime 类型还是什么?
也试过这个:
[manager POST:path parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"[UploadVC] success = %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"[UploadVC] error = %@", error);
}];
和这个:
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:path parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFormData:imageData name:@"photo"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"[UploadVC] success = %@", responseObject);
[self blockView:self.view block:NO];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"[UploadVC] error response.object = %@", operation.responseObject);
[self blockView:self.view block:NO];
}];
没有任何工作。希望有人可以提供帮助,因为我一直坚持下去,并在 SO
tia上从一个问题到另一个问题
编辑:新尝试
1)首先是多部分表单
2)创建上传任务
它们都不适合我,所以我仍在努力解决这个问题,但看不到任何解决方案