【发布时间】:2013-11-19 03:03:31
【问题描述】:
我正用这个把头撞到墙上。我想从库中选择UIImage 并将其上传到服务器,就像使用<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) 创建上传任务
他们都没有为我工作,所以我仍在努力解决这个问题,但看不到任何解决方案
【问题讨论】:
-
失败块上[操作响应字符串]的日志是什么?
-
你的意思是第一个例子?它是来自服务器的 html 文件,其中包含错误信息(来自coldfusion)
-
其实真的很乱,但从中我可以找到诊断部分说:表单字段照片不包含文件
-
不知道这个参数有什么问题 [formData appendPartWithFileData:imageData name:@"public.image" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
标签: ios objective-c file-upload uiimageview afnetworking-2