2016-03-03 69 views
0

如何使用AFNetworking 3.0 uploadTaskWithRequest:fromFile:progress:completionHandler从磁盘实现带有附加信息的上传文件(在本例中为caption & channel_id)以利用新的后台会话上传?AFNetworking 3.0如何上传附加数据文件?

我打开替代解决方案,如果它与AFNetworking和后台文件上传。

这里是我试过的服务器400错误的请求或500内部处理错误(在做卷曲一个多窗体发布作品)回应说:

@property (nonatomic, strong) AFURLSessionManager *uploadSession; 

// Configured uploadSession as backgroundSession configuration with couple of headers 

- (void)uploadMediafileFilename:(NSString *)filename 
       filenameWithPath:(NSString *)filenameWithPath 
         progress:(void (^) (NSProgress *uploadProgress))progress 
         caption:(NSString *)caption 
         channel:(NSString *)channelId 
       completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionBlock { 

// --------- 
// Create Request Method #1 

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@/", kAPIBaseURL, kAPIMediafilesURL]]]; 

// Tried dictionary to JSON in body. Also tried a params string version. 

    NSDictionary *dict = @{ @"channel_id" : channelId, 
          @"mediafile" : @{ @"filename" : filename, @"caption" : caption } }; 

    request.HTTPBody = [NSKeyedArchiver archivedDataWithRootObject:dict]; 
    request.HTTPMethod = @"POST"; 


// ---- OR ---- 
// Create Request Method #2 

    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:[NSString stringWithFormat:@"%@%@", kAPIBaseURL, kAPIMediafilesURL] parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { 
     [formData appendPartWithFormData:[caption dataUsingEncoding:NSUTF8StringEncoding] name:@"mediafile[caption]"]; 
     [formData appendPartWithFormData:[channelId dataUsingEncoding:NSUTF8StringEncoding] name:@"channel_id"]; 
     [formData appendPartWithFileURL:fileURL name:@"mediafile[filename]" fileName:filename mimeType:@"image/jpeg" error:nil]; 
    } error:nil]; 

// ---------- 

    NSURL *fileURL = [NSURL fileURLWithPath:filenameWithPath]; 
    [request setValue:[self token] forHTTPHeaderField:kTokenHeaderField]; 


// Tried using uploadTaskWithRequest - prefer due to its background capabilities. Also tried uploadTaskWithStreamedRequest. 

    NSURLSessionUploadTask *uploadTask = [self.uploadSession uploadTaskWithRequest:request fromFile:fileURL progress:progress completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) { 

     if (error) { 
      NSLog(@"%@", [response description]); 
     } 

     if (completionBlock) { 
      completionBlock(response, responseObject, error); 
     } 
    }]; 
    [uploadTask resume]; 
} 
+0

我认为这是由于错误的参数传递到API调用文件上传 – iMHitesh

+0

我也这么认为......所以正确获取参数的正确方法是什么?任何关于上述方法如何将带有文件的参数编码为请求的方法都可以帮助我调试它。 – HM1

+0

亲爱的,我们只能显示我们的网址和数据,随它发送 – iMHitesh

回答

1

我得到这个使用下面的工作:

  1. 配置AFURLSessionManager *uploadSessiondefaultSessionConfiguration
  2. 使用上述方法#2设置NSMutableURLRequest
  3. 使用AFNetworking的 uploadTaskWithStreamedRequest:progress:completionHandler:

底线:backgroundSessionConfiguration不流多部分的形式工作,我无法将包含在uploadTaskWithRequest:fromFile:附加数据。