我開發了一個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];
}
你的後端平臺PHP/.NET /等? – laxonline
@laxonline它是php – user2877853
檢查您的PHP ini文件「upload_max_filesize」 – laxonline