2012-11-07 32 views
0

我正在使用AFNetworking并想上传一个NSData对象到我的服务器。我试图上传到我们的SOAP服务器,所以我有XML,我已经转换为NSData对象。出于某种原因,后续不工作:AFNetworking上传进度发送SOAP NSData

// Back to NSData 
    NSData *convertedFile = [xml dataUsingEncoding:NSUTF8StringEncoding]; 

    NSURL *url = [NSURL URLWithString:siteConfiguration.soapURL]; 
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; 
    NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { 
     [formData appendPartWithFormData:convertedFile name:assetCreation.name]; 
    }]; 

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { 

     // Progress 
     float totalProgress = (float)totalBytesWritten/(float)totalBytesExpectedToWrite; 
     progress(totalProgress, totalBytesWritten, totalBytesExpectedToWrite); 
     if (totalBytesExpectedToWrite == totalBytesWritten) { 

      completion(); 

     } 
    }]; 
    [operation start]; 

但不使用multipartFormRequestWithMethod块时,它的工作和文件正确上传,但没有显示出有进步,回调只是调用一次:

// Back to NSData 
NSData *convertedFile = [xml dataUsingEncoding:NSUTF8StringEncoding]; 

NSURL *url = [NSURL URLWithString:siteConfiguration.soapURL]; 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; 
[request setHTTPMethod: @"POST"]; 
[request setHTTPBody:convertedFile]; 

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { 

    // Progress 
    float totalProgress = (float)totalBytesWritten/(float)totalBytesExpectedToWrite; 
    progress(totalProgress, totalBytesWritten, totalBytesExpectedToWrite); 
    if (totalBytesExpectedToWrite == totalBytesWritten) { 

     MatrixAsset *asset = [[MatrixAsset alloc] init]; 
     completion(asset); 

    } 
}]; 
[operation start]; 

看来,我的服务器真的想与我NSData对象发送HTTPBody。是否有可能对AFNetworking进度回调做同样的事情?

+0

你试过'[FORMDATA appendPartWithFileData:convertedFile名称:assetCreation.name];'而不是'[FORMDATA appendPartWithFormData:convertedFile name:assetCreation.name];'? – mkral

+0

另外我会设置'mimeType:'为'text/xml' – mkral

+0

没有一种叫做[formData appendPartWithFileData:convertedFile name:assetCreation.name]的方法,至少不在最新版本的AFNetworking中。我需要将它作为我认为的形式发送,因为它发送给我们的SOAP服务器,并且正在发送其他字段数据。 –

回答

0

好的,我得到了它的工作。看来我错了绕了:

// Setup the connection 
    NSString *wsdlURL = [NSString stringWithFormat:@"%@?WSDL", siteConfiguration.soapURL]; 
    NSURL *serviceUrl = [NSURL URLWithString:wsdlURL]; 
    NSMutableURLRequest *serviceRequest = [NSMutableURLRequest requestWithURL:serviceUrl]; 
    [serviceRequest setValue:@"text/xml" forHTTPHeaderField:@"Content-type"]; 
    [serviceRequest setHTTPMethod:@"POST"]; 
    [serviceRequest setHTTPBody:[xml dataUsingEncoding:NSUTF8StringEncoding]]; 

    // Operation 
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:serviceRequest]; 
    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { 

     // Progress 
     float totalProgress = (float)totalBytesWritten/(float)totalBytesExpectedToWrite; 
     progress(totalProgress, totalBytesWritten, totalBytesExpectedToWrite); 

    }]; 
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSLog(@"Success"); 
     MatrixAsset *asset = [[MatrixAsset alloc] init]; 
     completion(asset); 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"error: %@", error.localizedDescription); 
     NSLog(@"error: %@", error.localizedFailureReason); 
    }]; 
    [operation start]; 
0

您需要的SOAPAction继续:

[serviceRequest setValue:@"SOAP_ACTION_URL" forHTTPHeaderField:@"SOAPAction"];