使用Afnetworking的图像上传在ios8中不起作用

时间:2014-10-16 12:27:41

标签: ios afnetworking

我在ios7中完成了多个图片上传。但它在ios 8中不起作用。我在下面显示了我的代码。请建议我该怎么办?

NSString *url = [NSString stringWithFormat:@"%@abc.php", SERVERNAME]; [manager POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData)  {
        for (int i = 0; i < filesCount; i++) {
            NSString *imgName = [NSString stringWithFormat:@"image%d", (int)(i + 1)];
            NSData *imgData = [[NSData alloc] initWithContentsOfFile:[dataPath stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@", filelist[i]]]];
            [formData appendPartWithFileData:imgData name:imgName fileName:filelist[i] mimeType:@"image/png"];
        }
         } success:^(AFHTTPRequestOperation *operation, id responseObject) {  NSLog("getting response");      } Failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //NSLog(@"Error: %@ ***** %@", operation.responseString, error);

    }];

调试点不是成功的部分。 你能帮我解决一下这个问题。

提前致谢。

2 个答案:

答案 0 :(得分:0)

您可以使用以下代码从POST请求中获取响应

1在项目或NSJSONSerialization类中添加SBJSON类以编码数据

    NSURL *url = [[NSURL alloc]initWithString:@"You -server -url "];

    NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

    // Server parameter dict

    NSDictionary *jsonDict = [[NSDictionary alloc] initWithObjectsAndKeys:txtUserName.text,@"Email",txtPassword.text,@"Password",[appDelegate uniqueIDForDevice],@"DeviceToken",@"ios",@"DeviceType", nil];

    NSString *jsonRequest = [jsonDict JSONRepresentation];
    NSData *requestData = [jsonRequest dataUsingEncoding:NSUTF8StringEncoding];

    [theRequest setHTTPMethod:@"POST"];
    [theRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [theRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [theRequest setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPBody: requestData];


    [NSURLConnection sendAsynchronousRequest:theRequest queue:[NSOperationQueue currentQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
         NSError *err = nil ;

         NSDictionary *dict=  [NSJSONSerialization JSONObjectWithData :data options : 0 error :&err];

      NSLog(@"%@",dict);
     }];        
}

答案 1 :(得分:0)

我尝试过这种方法

 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

    NSURL *baseUrl = [NSURL URLWithString:url];

    NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
    [request setURL:baseUrl];
    [request setHTTPMethod:@"POST"];

    NSString *boundary = @"0xKhTmLbOuNdArY";
    NSString *endBoundary = [NSString stringWithFormat:@"\r\n--%@\r\n", boundary];

    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; charset=%@; boundary=%@", charset, boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    NSMutableData *tempPostData = [NSMutableData data];
    [tempPostData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];



    for(int i=0;i<keys.count;i++){
        NSString *str = values[i];
        NSString *key =keys[i];
        NSLog(@"Key Value pair: %@-%@",key,str);
        [tempPostData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
        [tempPostData appendData:[str dataUsingEncoding:NSUTF8StringEncoding]];
        // [tempPostData appendData:[@"\r\n--%@\r\n",boundary dataUsingEncoding:NSUTF8StringEncoding]];
        [tempPostData appendData:[endBoundary dataUsingEncoding:NSUTF8StringEncoding]];

    }

    //semd multiple images to server
    for (int i = 0; i < filesCount; i++) {

        [tempPostData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

        if (i == 0)
        {
            [tempPostData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image1\"; filename=\"%@\"\r\n", filelist[0]] dataUsingEncoding:NSUTF8StringEncoding]];
            [tempPostData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
            [tempPostData appendData:imageData1];
            [tempPostData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        }

        else if (i == 1)
        {
            [tempPostData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image2\"; filename=\"%@\"\r\n", filelist[1]] dataUsingEncoding:NSUTF8StringEncoding]];
            [tempPostData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
            [tempPostData appendData:imageData2];
            [tempPostData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        }
        else
        {
            [tempPostData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image3\"; filename=\"%@\"\r\n", filelist[2]] dataUsingEncoding:NSUTF8StringEncoding]];
            [tempPostData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
            [tempPostData appendData:imageData3];
            [tempPostData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        }
        //        [tempPostData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        //        [tempPostData appendData:imgData];

    }




    [tempPostData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:tempPostData];
    _receivedData = [NSMutableData dataWithCapacity: 0];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if( theConnection )
    {

        NSLog(@"request uploading successful");


    }
    else
    {
        _receivedData = nil;
        NSLog(@"theConnection is NULL");
    }

这对我有用。它适用于上传多个图像。并且Afnetworking在ios8中仍然存在问题。

谢谢