2

第 1 步:我在这里创建请求

NSMutableURLRequest *request1 = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST"
                                                                                           URLString:[NSString stringWithFormat:@"%@%@", API_MAIN_URL, IMAGE_UPLOAD]
                                                                                          parameters:param constructingBodyWithBlock:^(id formData) {

           [formData appendPartWithFileURL:[NSURL fileURLWithPath:strImagePath] 
                                      name:@"sendimage" 
                                  fileName:[strImagePath lastPathComponent] 
                                  mimeType:@"image/png"
                                     error:nil];
                                       } error:nil];
[request1 setValue:authToken forHTTPHeaderField:@"Authorization"];

第2步:在这里我在给定位置创建流

[[AFHTTPRequestSerializer serializer] requestWithMultipartFormRequest:request1 
                                          writingStreamContentsToFile:[NSURL fileURLWithPath:[strImagePath stringByDeletingPathExtension]] 
                                                    completionHandler:^(NSError *error){

第3步:在这里我正在创建上传任务。

        ///here is file
        NSProgress *progress = nil;
        NSURLSessionUploadTask *uploadTask = [self.manager uploadTaskWithRequest:request1
                                                                        fromFile:[NSURL fileURLWithPath:strImagePath]
                                                                        progress:&progress
                                                               completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
                                                                       NSLog(@"response : %@\n\n responseObject : %@\n\n error : %@", response, responseObject, error);

                                                               }];
        [uploadTask resume];
    }
                                                    }];
}

我的问题是在应用程序进入后台模式之前,我想HTTPBody使用步骤:1 和步骤:2 在给定位置写入所有请求(HTTPStream),并NSArray在写入所有HTTPStream文件(在应用程序前台中)意味着之后将所有请求保存到同时我将显示图像准备上传。

然后我将在 Step3 的帮助下开始创建后台任务:将请求传递给我已存储到NSArray. 使用这种方法,我无法上传图片。

但是如果我将所有步骤一个一个地调用,那么它将把图像上传到服务器上,但是通过这种方法,我的应用程序应该在前台进行创建请求,因为我们需要HTTPBody在给定位置编写。

请帮我解决这个问题,我从上周开始就被困在这个问题上。我的应用程序需要通过服务器上传超过 500 张图片。

4

2 回答 2

0

试试这样

 NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"file://path/to/image.jpg"] name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil];
            } error:nil];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
        NSProgress *progress = nil;

        NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
            if (error) {
                NSLog(@"Error: %@", error);
            } else {
                NSLog(@"%@ %@", response, responseObject);
            }
        }];

        [uploadTask resume];
于 2015-05-15T05:39:51.617 回答
0

我已经完成了这项任务,创建所有上传任务而不恢复它们,并在创建后将每个任务保存在 NSArray 中。一旦创建了所有任务,然后调用一个我们恢复所有任务的方法。

于 2015-06-08T05:48:27.743 回答