上传使用AFNetworking [英] Upload Using AFNetworking

查看:188
本文介绍了上传使用AFNetworking的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  NSURL * url = [NSURL URLWithString:@http://127.0.0.1:8000/photo/]; 
AFHTTPClient * httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSDictionary * headers = [NSDictionary dictionaryWithObject:data forKey:@attachment];
// NSLog(@strimng);
NSMutableURLRequest * request = [httpClient multipartFormRequestWithMethod:@POSTpath:@upload /parameters:nil constructBodyWithBlock:^(id< AFMultipartFormData> formData){
NSLog(@strimng);
[formData appendPartWithFileData:data name:@attachmentfileName:@attachment.jpgmimeType:@image / jpeg];
NSLog(@strimng);
}];
NSLog(@strimng);
AFHTTPRequestOperation * operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[操作setUploadProgressBlock:^(NSUInteger bytesWritten,long long totalBytesWritten,long long totalBytesExpectedToWrite){
NSLog(@%lld bytes已发送%lld,totalBytesWritten,totalBytesExpectedToWrite);
}];
[操作开始];

我尝试使用AFNetworking从iPhone上传图像,但这不起作用。
在formdata appenddata命令之前的NSLog被注销。但之后的那个似乎不是。我也检查了NSdata是否为零,那也不是这种情况。而obv请求没有被发送。
请任何人都可以帮助我。

解决方案

假设您必须上传图片,为什么要创建 NSDictionary data ?不需要,只需在 multipartFormRequestWithMethod 中使用 data

请尝试以下操作:

  //通过ImagePicker获取想要上传的图片或者@property 
UIImage * imageIWantToUpload = self.image;

//为上传过程创建NSData对象
NSData * dataToUpload = UIImageJPEGRepresentation(imageIWantToUpload,0.5);

NSMutableURLRequest * request = [httpClient multipartFormRequestWithMethod:@POSTpath:@upload /parameters:nil constructBodyWithBlock:^(id< AFMultipartFormData> formData){
[formData appendPartWithFileData:dataToUpload name:@attachmentfileName:@attachment.jpgmimeType:@image / jpg];
}];

//你可以添加progressBlock和completionBlock
AFHTTPRequestOperation * operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[操作setUploadProgressBlock:进度];
[操作setCompletionBlockWithSuccess:成功失败:失败];

您还应该仔细检查您的服务器端是否需要一些参数,因为您正在设置 NSMutableURLRequest 参数'字段为 nil


NSURL *url = [NSURL URLWithString:@"http://127.0.0.1:8000/photo/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSDictionary *headers = [NSDictionary dictionaryWithObject:data forKey:@"attachment"];
//NSLog(@"strimng");
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"upload/" parameters:nil constructingBodyWithBlock: ^(id<AFMultipartFormData> formData) {
    NSLog(@"strimng");
    [formData appendPartWithFileData:data name:@"attachment" fileName:@"attachment.jpg" mimeType:@"image/jpeg"];
    NSLog(@"strimng");
}];
NSLog(@"strimng");
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[operation start];

I am Trying to upload the image from the iphone using AFNetworking, but this is not working. The NSLog just before formdata appenddata command is logged out. But the one after that doesnt seem to. I have also checked if NSdata is nil and thats also not the case. And obv the request is not being sent. Please can anyone help me.

解决方案

Assuming that you have to upload an image, why are you creating an NSDictionary with the data? It is not necessary, you just need to use data inside multipartFormRequestWithMethod.

Try the following

// Get the image that you want to upload via ImagePicker or a @property
UIImage *imageIWantToUpload = self.image;

// Create the NSData object for the upload process
NSData *dataToUpload = UIImageJPEGRepresentation(imageIWantToUpload, 0.5);

NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"upload/" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:dataToUpload name:@"attachment" fileName:@"attachment.jpg" mimeType:@"image/jpg"];
}];

// You can add then the progressBlock and the completionBlock
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:progress];
[operation setCompletionBlockWithSuccess:success failure:failure];

You should also double check if your server side expects some parameters, because you are setting the NSMutableURLRequest parameters' field to nil.

这篇关于上传使用AFNetworking的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆