通过AFNetworking上传PowerPoint会损坏

时间:2014-05-08 15:36:30

标签: objective-c afnetworking powerpoint corrupt

通过以下代码上传PowerPoint时收到成功回复。它确实上传但文件已损坏。通过PowerPoint打开服务器上的损坏文件时,我收到此消息:

“PowerPoint发现filename.pptx中的内容存在问题.PowerPoint可以尝试修复演示文稿。”

- (void)updateDocument:(NSString *) path parameters:(FileUploadParameters*)para success:(void (^)(void))success failure:(void (^)(NSError *error))failure
{
    _postData = nil;
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:[ConfigurationUtil objectForKey:@"baseURL"]]];


    _postData = [NSMutableData dataWithContentsOfFile:[path stringByStandardizingPath]];

    _postData = [NSMutableData dataWithContentsOfFile:[path stringByStandardizingPath] options:NSDataReadingMapped error:nil];
    NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:HTTP_METHOD_POST
                                                                         path:[self getQueryString:path parameter:para]
                                                                   parameters:nil
                                                    constructingBodyWithBlock: ^(id <AFMultipartFormData>formData)
                                                    {
                                                        [formData appendPartWithFileData:_postData
                                                                                    name:[[path lastPathComponent] stringByDeletingPathExtension]
                                                                                fileName:[[path lastPathComponent] stringByDeletingPathExtension]                                                                                mimeType:@"application/powerpoint"];


                                                    }];

    [request addValue:[NSString stringWithFormat:@"WRAP access_token=%@",[Tenant loadSharedTenantInstance].authToken] forHTTPHeaderField:@"Authorization"];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
         {
             if (success)
              success();
         }
     failure:^(AFHTTPRequestOperation* operation, NSError* error)
         {
             if (failure)
                failure(error);
         }
     ];
    [operation start];
}

2 个答案:

答案 0 :(得分:1)

您的AFNetworking代码看起来很好。我不认为那是罪魁祸首。

您可以使用cmp命令行工具逐字节比较两个文件(原始文件和上传文件),看看是否存在差异。我想不会有。

更有可能:这是一个误导性的错误消息,它实际上是安装了Powerpoint的计算机上的权限问题,如this Microsoft Knowledge Base article中所述。

答案 1 :(得分:0)

以下代码更正了问题。似乎appendPartWithFileData导致了问题。它更改了文件大小,似乎将文件注册为损坏。

_postData = nil;     _postData = [NSMutableData dataWithContentsOfFile:[path stringByStandardizingPath]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
NSString *queryString =[self getQueryString:path parameter:nil];
[request setURL:[NSURL URLWithString:queryString]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:_postData];

[request addValue:[NSString stringWithFormat:@"WRAP access_token=%@",[Tenant loadSharedTenantInstance].authToken] forHTTPHeaderField:@"Authorization"];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         if (success)
          success();
     }
 failure:^(AFHTTPRequestOperation* operation, NSError* error)
     {
         if (failure)
            failure(error);
     }
 ];
[operation start];