2014-01-06 27 views
1

我開發了一個ios應用程序,並用於製作服務器請求,我使用的是ASIHTTPRequest。我面臨的問題是服務器請求超時,當我嘗試上傳大型文件。我已經嘗試了SetData和SetFile方法,但是兩者的問題都是一樣的。我無法解決它。它吃了我的四天。我的代碼如下。提前致謝。使用ASIFormDataRequest將大型文件上傳到服務器時請求超時

- (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]; 
} 
+0

你的後端平臺PHP/.NET /等? – laxonline

+0

@laxonline它是php – user2877853

+0

檢查您的PHP ini文件「upload_max_filesize」 – laxonline

回答

0

添加此,

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

設置您需要的最大秒數。

+0

感謝您的回覆。但我也嘗試過這種做法。這樣文件上傳會在特定的時間和時間後重新啓動。最終 – user2877853

0

不同情況下可能會出現問題,你可能會發現它很有用嘗試以下步驟

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

upload_max_filesizepost_max_size在php.ini:

; 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

+0

我認爲通過RestClient,可以測試那些以Json形式出現的請求。我的請求是多部分,可以在RestClient上測試多部分請求嗎?如果是,請告訴我我能做到的方式。謝謝 – user2877853

+0

首先你可以檢查你的PHP ini文件「upload_max_filesize」 – laxonline

+0

是的,它的最大尺寸遠遠大於我試圖上傳的文件 – user2877853

相關問題