AFnetworking 2.2.0在服务器问题上传图像 [英] AFnetworking 2.2.0 upload image on server issues

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

问题描述

之所以要求你这么做,是因为我对params感到困惑。

The reason why ask you have to do it because I confuse about params.

据我所知,有一种方法可以使用多部分请求。

As I understood there one way how to do it using multi part request.

这种方式为我们提供了两个概念,因为我理解从文件上传,可以存储在Document目录中或使用NSData对象。

This way offers us two concepts as I understood upload from file that can be stored in Document directory or using NSData object.

从文件上传:

所以我已将test.jpg保存到文档目录中。然后我让NSURL实例在多部分请求中使用它。下面的代码显示了我如何创建NSURL实例:

So I have saved test.jpg to the document directory. Then I make NSURL instance to use it in multi part request. The code below shows how I create NSURL instance:

NSString *fileName = @"test.jpg";
NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSURL *filePath = [NSURL fileURLWithPath:folderPath];

当我打印文件路径时:

file:///Users/mac/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/F732FCF6-EAEE-4E81-A88B-76ADB75EDFD1/Documents/test.jpg

然后我设置我的参数并使用formData附加我的文件,如下所示:

Then I set my parameters and using formData to attach my file as below:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
[manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileURL:filePath name:@"image" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

这是正确的吗?或者我错过了什么,因为回复不成功?

Is this correct? or maybe I miss something because the response is not success?

第二个概念 - 直接处理数据:

The second concept - work directly with data:

[formData appendPartWithFileData:imageData name:@"image" fileName:@"test.jpg" mimeType:@"image/jpeg"];

但是当应用程序调用这行代码时出现错误:

but when the app invokes this line of code I got the error:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: body'

我认为在管理器块外定义的imageData在此行上为零。所以我在这里需要帮助以及如何将它传递到块中。

The reason I suppose that the imageData defined outside the manager block is nil on this line. So I need help here as well how to pass it into the block.

请你纠正我的步骤中的错误,也许我会错过一些东西。

Please can you correct me what's wrong in my steps maybe I miss something.

此外,当我评论该行

[formData appendPartWithFileData:imageData name:@"image" fileName:@"test.jpg" mimeType:@"image/jpeg"];

[formData appendPartWithFileURL:filePath name:@"image" error:nil];

然后一切正常,我在重新运行时获得成功回复。

then everything works fine and I get success response in rerun.

推荐答案

您是否确认在Documents文件夹中的该位置找到了该文件?我也无法将以编程方式确定的文件路径(这是正确的)与字符串文字文件路径(很容易出问题)进行协调。您应始终以编程方式确定路径,而不是使用字符串文字(因为当您重新安装应用程序时,该路径将更改)。我认为你需要的是:

Have you confirmed that the file is found at that location in your Documents folder? I'm also having trouble reconciling your programmatically determined file path (which is correct) with your string literal file path (which can easily be problematic). You should always programmatically determine the path, not using string literals (because when you reinstall the app, that path will change). What I think you need is something like:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"image.png"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];

[manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    NSError *error;
    BOOL success = [formData appendPartWithFileURL:fileURL name:@"image" fileName:filePath mimeType:@"image/png" error:&error];
    if (!success)
        NSLog(@"appendPartWithFileURL error: %@", error);
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

注意,这以编程方式确定 filePath (这是一个路径,而不是URL),然后使用 fileURLWithPath 将其转换为文件URL。它还确认是否成功,如果不成功则记录错误。

Note, this programmatically determines the filePath (which is a path, not a URL), and then uses fileURLWithPath to convert that to a file URL. It also confirms whether it was successful or not, logging the error if not successful.

另请注意,这假设您的服务器将其响应作为JSON返回。如果没有,你必须更改 responseSerializer

Also note that this assumes your server is returning its response as JSON. If not, you'd have to change the responseSerializer.

这篇关于AFnetworking 2.2.0在服务器问题上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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