rai*_*lin 33 file-upload objective-c uiimageview ios afnetworking-2
我用这个把头撞在墙上.我想UIImage
从库中选择并将其上传到服务器,就像可以做到的那样<form action="http://blabla.request.cfm" method="post" enctype="multipart/form-data">
.而不是成功我得到了这个错误:
error = Error Domain = NSCocoaErrorDomain Code = 3840"操作无法完成.(Cocoa error 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];
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用......服务器说没有文件.不确定加密是否错误,mime类型还是什么?
也试过这个:
[manager POST:path parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"[UploadVC] success = %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"[UploadVC] error = %@", error);
}];
Run Code Online (Sandbox Code Playgroud)
还有这个:
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];
}];
Run Code Online (Sandbox Code Playgroud)
什么都没有用.希望有人能帮助,因为我坚持了下来,并从问题盘旋在此问题上的SO
TIA
编辑:新尝试
1)首先是多部分形式
2)创建上传任务
他们都没有为我工作,所以我仍然试图应对,但看不到任何解决方案
rai*_*lin 94
我不确定哪个部分(我认为缺少一些细节)是负责任的,但我最后做了:)在这里你去:
-(void)uploadPhoto{
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://server.url"]];
NSData *imageData = UIImageJPEGRepresentation(self.avatarView.image, 0.5);
NSDictionary *parameters = @{@"username": self.username, @"password" : self.password};
AFHTTPRequestOperation *op = [manager POST:@"rest.of.url" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//do not put image inside parameters dictionary as I did, but append it!
[formData appendPartWithFileData:imageData name:paramNameForImage fileName:@"photo.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];
[op start];
}
Run Code Online (Sandbox Code Playgroud)
奇迹般有效 :)
归档时间: |
|
查看次数: |
44831 次 |
最近记录: |