2014-05-08 66 views
0

通过下面的代码上传PowerPoint时,我收到了成功的响应。它确实上传但文件已损坏。当通过PowerPoint打开服务器上的损坏文件时,我收到以下消息:通过AFNetworking上传PowerPoint得到损坏

“PowerPoint发现文件名.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]; 
} 
+1

这些文件究竟有什么不同?他们有不同的大小或一些字节被更改?当试图找到一个错误时,不要只看代码。 – Sulthan

+0

您的成功或失败块被叫?我想知道数据是否没有完成发送,因为操作在方法结束时超出了范围。我一直遵循单身HTTP客户端的推荐模式,并将操作添加到其操作队列中。 –

+0

这些文件看起来不一样,它们的大小相同。他们只是被标记为腐败。 PowerPoint中的修复按钮修复文件,但我需要该文件不被视为损坏。使用相同的代码可以上传没有损坏但不包含微软文档(即Excel,Word,PowerPoint)的PDF。成功块被调用。我得到了成功的回应。 – rosieBCPD1

回答

1

你的AFNetworking代码看起来不错。我不认为这是罪魁祸首。

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

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

+0

谢谢,我会研究这个,并让你知道它是否有效。字节是不同的,但appendPartWithFileData将做到这一点。但是,这不应该破坏其他任何东西。 – rosieBCPD1

+0

@ user2969771让我们知道发生了什么! –

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