AFnetworking 2.2.0在服务器问题上传图片

之所以要你这样做,是因为我混淆了params。

正如我理解有一种方法如何使用多部分请求。

这种方式为我们提供了两个概念,据我所知,从file upload可以存储在文档目录或使用NSData对象。

从file upload:

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

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

当我打印文件path:

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

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

 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); }]; 

它是否正确? 或者我错过了一些东西,因为回应不成功?

第二个概念 – 直接使用数据:

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

但是当应用程序调用这行代码时,我得到了错误:

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

我认为在pipe理器块外定义的imageData在这一行上是无效的。 所以我需要帮助,以及如何将它传递给块。

请你能纠正我的错误,也许我错过了一些事情。

另外当我评论这一行

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

要么

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

那么一切正常,我得到成功的反应在重新运行。

您是否确认在您的文档文件夹中的该位置find该文件? 我也遇到麻烦协调您的程序确定的文件path(这是正确的)与您的string文字path(这可能很容易成为问题)。 您应该总是以编程方式确定path,而不是使用string文字(因为当您重新安装应用程序时,该path将会改变)。 我认为你需要的是这样的:

 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 (这是一个path,而不是一个URL),然后使用fileURLWithPath将其转换为一个文件的URL。 它也确认是否成功,如果不成功则logging错误。

另外请注意,这假定你的服务器返回的响应为JSON。 如果没有,你必须改变responseSerializer