如何使用AFNetworking发布图像?

时间:2013-11-20 15:14:24

标签: ios afnetworking

在我的应用程序中,我使用AFNetworking来解析服务。在这里,我必须将图像发布到服务器,所以当试图发布图像时,我收到以下错误消息作为响应。

这是我的代码:

NSString *urlString = [NSString stringWithFormat:@"http://www.xyz.com:8084/TappyokaCmsService/Service.svc"];

NSString *path=[NSString stringWithFormat:@"/InsertUserInfo?User_Name=%@&User_Pwd=%@&User_FName=%@&User_LName=%@&User_Mail=%@&User_Phno=%@&User_Address=%@&User_Dob=%@&Ic_No=%@&User_Active=%@&User_Questions=%@&User_Ans=%@&ImageUrl=%@&IsSocial=%@",@"skull",@"skull",@"skull",@"skull",@"skull",@"skull",@"skull",@"skull",@"skull",@"10001",@"skull",@"skull",@"",@"10001"];

AFHTTPClient *httpClient= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:urlString]];    [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];   [AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];   NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:path parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageToUpload name:@"skull" fileName:@"skull.jpg" mimeType:@"image/jpg"];
}];

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

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSDictionary *jsons = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
         NSLog(@"response: %@",jsons);

     }
    failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         if([operation.response statusCode] == 403)
         {
             NSLog(@"Upload Failed");
             return;
         }
         NSLog(@"error: %@", [operation error]);

     }];

    [operation start];

错误信息是:

Status code: 404, headers {
"Content-Length" = 1245;
"Content-Type" = "text/html";
Date = "Wed, 20 Nov 2013 15:04:17 GMT";
Server = "Microsoft-IIS/7.5";
"X-Powered-By" = "ASP.NET"

1 个答案:

答案 0 :(得分:0)

404错误

404错误是由您尝试访问服务器找不到的Web服务上的某些内容引起的,即您正在调用错误的URL。

<强>文档

希望有一些关于你应该如何与网络服务进行交互以便上传照片的文档,我感觉你做错了(如果我没有弄错你应该发出SOAP请求?)。