我也希望将图像上传到服务器但是在执行此操作时出错
我使用了以下代码,但不知道哪里有错误
+(void) HTTPPostImage:(NSString *) stringURL andParameter:(NSData *) imageData andSelector:(SEL) selector andTarget:(id) target{
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:stringURL]];
// NSData *imageData = UIImageJPEGRepresentation(self.avatarView.image, 0.5);
NSDictionary *parameters =NULL;
NSString *myUUID = [[NSUUID UUID] UUIDString]; // create a UUID
NSLog(@"myUUID%@",myUUID);
AFHTTPRequestOperation *op = [manager POST:@"" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"public.image" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
//[formData appendPartWithFileData:imageData name:@"file" fileName:myUUID 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];
}
我不知道此参数
中有什么问题[formData appendPartWithFileData:imageData name:@"public.image" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
对于服务器参数
我不知道我是否犯过错误或服务器已经完成了
答案 0 :(得分:0)
添加您的网址最后一个组件名称并更改您的代码:
AFHTTPRequestOperation *op = [manager POST:@"suburl.php" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"userfile" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
//[formData appendPartWithFileData:imageData name:@"file" fileName:myUUID mimeType:@"image/jpeg"];
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://yourbaseurl.com/"]];
// NSData *imageData = UIImageJPEGRepresentation(self.avatarView.image, 0.5);
NSDictionary *parameters =NULL;
NSString *myUUID = [[NSUUID UUID] UUIDString]; // create a UUID
NSLog(@"myUUID%@",myUUID);
AFHTTPRequestOperation *op = [manager POST:@"suburl.php" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"userfile" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
//[formData appendPartWithFileData:imageData name:@"file" fileName:myUUID 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];
答案 1 :(得分:0)
请尝试以下代码。希望它为你工作。
NSData *tempData = [NSData dataWithContentsOfFile:YOUR_IMAGE_FILE_PATH];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:YOUR_REQUEST_URL parameters:YOUR_PARAMETERS constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if (tempData) {
[formData appendPartWithFileData:tempData name:@"user_image" fileName:@"avatar.jpeg" mimeType:@"image/jpeg"];
}
} success:^(AFHTTPRequestOperation *operation, id responseObject){
NSError *error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:operation.responseData
options:kNilOptions
error:&error];
NSLog(@"Registration JSON:- %@",json);
}failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(@"Error: %@", error);
}];
“user_image”是图像的关键参数。此密钥在API请求参数中设置。
谢谢:)