上传图片时出现问题 [英] Issue while uploading Image

查看:177
本文介绍了上传图片时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

步骤1 :在这里我正在创建请求

Step 1: here I am creating the Request

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:在这里,我正在给定位置创建流 p>

Step 2: here I am creating the Stream at given Location

[[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 (HTTPStream),使用步骤1和步骤2,并将所有请求保存到 NSArray 在文件(在应用程序前景中)中写入所有 HTTPStream 之后,我将显示图像准备上传。

My problem is before application going to background mode I want to write all the request HTTPBody(HTTPStream) at given location with using Step:1 and Step:2 and save all the request into the NSArray after writing all the HTTPStream in File(in application Foreground) mean while I'll show Image prepare to upload.

然后,我将在步骤3的帮助下开始创建后台任务:将请求传递到该请求中,该请求已存储到 NSArray 中。
用这种方法我无法上传图像。

then I'll start creating background task with the help of Step3: with passing request into this which request I have stored into the NSArray. with this approach I am not able to upload images.

但是如果我将所有步骤一一调用,它将在服务器上上传图像,但是使用这种方法,我的应用程序应该在Foreground中创建请求,因为我们需要在给定位置编写 HTTPBody

But If I call all the steps together one by one then it will upload the image on the server, but with this approach my application should be in Foreground for create request because we need to write the HTTPBody at give location.

请帮助我解决这个问题,我从上两周开始就一直对此感到困惑。
我的应用程序需要在服务器上上传超过500张图像。

Please help me to come out this problem I am stuck on this from last 2 week. My application needed more then 500 images uploading over the server.

推荐答案

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

I have done with this task, create all the upload task without resume them and save every task within the NSArray after creation. Once all the task are created then call a method in which we resume all the tasks.

这篇关于上传图片时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆