AFNetworking上传文件 [英] AFNetworking Uploading a file

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

问题描述

是否有人使用AFNetworking完全上传文件。我在互联网上找到了一些代码,但它不完整。我找到的代码在这里:

Does any one have a full implementation of uploading a file using AFNetworking. I have found some code on the internet but it is incomplete. The code I have found is here:

AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://my.client.server.com"]];


NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:[fieldName text] forKey:@"field01_nome"];
[parameters setObject:[fieldSurname text] forKey:@"field02_cognome"];



NSMutableURLRequest *myRequest = [client multipartFormRequestWithMethod:@"POST" path:@"/Contents/mail/sendToForm.jsp" parameters:parameters constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:myNSDataToSend mimeType:@"image/jpeg" name:@"alleagto"];
}];


AFHTTPRequestOperation *operation = [AFHTTPRequestOperation HTTPRequestOperationWithRequest:myRequest success:^(id object) {
    NSLog(@"Success");

} failure:^(NSHTTPURLResponse *response, NSError *error) {
    NSLog(@"Fail");

}];


[operation setUploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
    NSLog(@"Sent %d of %d bytes", totalBytesWritten, totalBytesExpectedToWrite);

}];

queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:operation];

我在以下几行收到错误:

I get errors on the following lines:

 [formData appendPartWithFileData:myNSDataToSend mimeType:@"image/jpeg" name:@"alleagto"];

在myNSDataToSend。

At the myNSDataToSend.

在这里:

AFHTTPRequestOperation *operation = [AFHTTPRequestOperation HTTPRequestOperationWithRequest:myRequest success:^(id object) {
    NSLog(@"Success");

} failure:^(NSHTTPURLResponse *response, NSError *error) {
    NSLog(@"Fail");

}];

错误是:

类方法'+ HTTPRequestOperationWithRequest:成功:失败'未找到(返回类型默认为'id')

Class method '+HTTPRequestOperationWithRequest:success:failure' not found(return type defaults to 'id')

任何有关此问题的帮助以及使用AFNetworks上传都会令人惊叹。

Any help on this and uploading with AFNetworks would be amazing.

谢谢。

推荐答案

首先,确保已下载最新版本的AFNetworking 。

First, make sure you have the latest version of AFNetworking downloaded.

AFHTTPRequestOperation + HTTPRequestOperationWithRequest:成功:失败:已删除几个版本。相反,您可以执行 [[AFHTTPRequestOperation alloc] initWithRequest:...] 然后设置 completionBlock 直接属性访问器( operation.completionBlock = ^ {...} ),或者带有 -setCompletionBlockWithSuccess:失败:。请记住,请求完成下载后会执行完成块。

AFHTTPRequestOperation +HTTPRequestOperationWithRequest:success:failure: was removed a few versions back. Instead, you can do [[AFHTTPRequestOperation alloc] initWithRequest:...] and then set the completionBlock with either the straight property accessor (operation.completionBlock = ^{...}), or with -setCompletionBlockWithSuccess:failure:. Keep in mind that completion blocks execute after the request has finished downloading.

对于多部分表单块, -appendWithFileData:mimeType:name 也被删除了一段时间。您想要的方法是 -appendPartWithFileData:name:fileName:mimeType:

As for the multipart form block, -appendWithFileData:mimeType:name was also removed a while back. The method you want is -appendPartWithFileData:name:fileName:mimeType:.

进行这两项更改,一切都应该有效。

Make those two changes and everything should work.

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

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