使用ASIFormDataRequest将大型文件上载到服务器时请求超时

时间:2014-01-06 08:00:24

标签: ios asihttprequest

我开发了一个ios应用程序,并且用于发出服务器请求我正在使用ASIHTTPRequest。我面临的问题是当我尝试上传大尺寸文件时服务器请求超时。我已经尝试过SetData和SetFile方法,但问题在于它们都是一样的。我无法解决它。它已经吃了我的四天。我的代码如下。提前谢谢。

- (void)syncTaskOnTheServer:(Tasks *)task ofTheUser:(NSString *)userId{

    __block unsigned long long uploaded = 0;
    ASIFormDataRequest *syncTask = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@tasks",baseurl]]];
    syncTask.shouldAttemptPersistentConnection = NO;
    [syncTask addPostValue:userId forKey:@"UserId"];
    [syncTask addPostValue:appId forKey:@"AppId"];
    [syncTask addPostValue:[NSString stringWithFormat:@"%@",task.taskTitle]    forKey:@"Title"];
    [syncTask addPostValue:[NSString stringWithFormat:@"%@",task.notes]        forKey:@"Description"];
    [syncTask addPostValue:[NSString stringWithFormat:@"%@",task.priority]     forKey:@"Priority"];
    [syncTask addPostValue:[NSString stringWithFormat:@"%@",task.taskCategory] forKey:@"TaskCategory"];
    [syncTask addPostValue:[NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@",task.creationDate]] forKey:@"CreationDate"];
    syncTask.shouldUseRFC2616RedirectBehaviour = YES;

    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    if (![task.imagePath isEqualToString:@"No Image Attached"]) {
        NSString *imageFilePath =  [NSString stringWithFormat:@"%@/%@_image.jpg",path,task.taskurlSubPart];
        UIImage *image          =  [UIImage imageWithContentsOfFile:imageFilePath];
        NSData *imageData       =  UIImageJPEGRepresentation(image, 1.0);
        NSLog(@"\n%@\n",task.taskurlSubPart);
        [syncTask setData:imageData withFileName:[NSString stringWithFormat:@"%@.jpg",task.taskurlSubPart ] andContentType:@"image/jpeg" forKey:@"Image"];
        /*
         [syncTask setFile:imageFilePath withFileName:[NSString stringWithFormat:@"%@.jpg",task.taskurlSubPart] andContentType:@"image/jpeg"  forKey:@"Image"];
         */
    }

    if (![task.audioURL isEqualToString:@"No Audio File"]) {
        NSMutableString *audioFileName = [NSMutableString stringWithString:task.audioFileName];
        [audioFileName appendString:@".caf"];
        NSString *audioFilePath = [NSString stringWithFormat:@"%@/%@.caf",path,task.audioFileName];
        NSData *audioData     = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:audioFilePath]];
        [syncTask setData:audioData withFileName:audioFileName andContentType:@"audio/x-caf" forKey:@"Audio"];
    }

    [syncTask setRequestMethod:@"POST"];

    [syncTask setStartedBlock:^{
        NSLog(@"start block");
    }];
    [syncTask setFailedBlock:^{
        NSLog(@"\n%@\n",[syncTask error]);
        NSString *serverResponse = [syncTask responseString];
        WLParseServerResponse *parseResponse = [[WLParseServerResponse alloc] init];
        [parseResponse parseSyncTaskResponse:serverResponse forTask:task];
        [parseResponse release];

    }];
    [syncTask setCompletionBlock:^{
        NSString *serverResponse = [syncTask responseString];
        NSLog(@"\nserver response: %@\n",serverResponse);
        WLParseServerResponse *parseResponse = [[WLParseServerResponse alloc] init];
        [parseResponse parseSyncTaskResponse:serverResponse forTask:task];
        [parseResponse release];
    }];
    [syncTask setHeadersReceivedBlock:^(NSDictionary *responseHeaders) {
        NSLog(@"%@", responseHeaders);
    }];
    [syncTask setBytesSentBlock:^(unsigned long long size, unsigned long long total) {
        uploaded += size;
        NSLog(@"%llu, %llu", uploaded, total);

    }];
    [syncTask startAsynchronous];
}

2 个答案:

答案 0 :(得分:0)

添加此内容,

[syncTask setTimeOutSeconds:45]; // 45秒,根据文件大小可能会有所不同

设置所需的最大秒数。

答案 1 :(得分:0)

在不同情况下可能会出现问题,您可能会发现尝试以下步骤很有用

Step 1 - If your using PHP backend service (Check your PHP INI file)

php.ini中的upload_max_filesizepost_max_size

; Maximum allowed size for uploaded files.
upload_max_filesize = 50M

; Must be greater than or equal to upload_max_filesize
post_max_size = 50M
  

第2步 - 通过休息客户端

测试相同的上传文件

Mozilla Add-ons RestClient

Chrome Advanced REST client