我是IOS的新手,正在使用NSURLConnection在服务器上上传视频文件。当我在服务器上传视频时,它返回状态代码为200但字典对象显示为:
Dictionary {
OK = 0;
info = "File not found.";
}
我完全厌倦了这一切,因为没有得到它的服务器问题或我身边的问题。我的代码是
NSArray *file=[[appDelegate.array_Allideos objectAtIndex:buttonTag] componentsSeparatedByString:@"."];
NSString *date=[file objectAtIndex:0];
NSString *extension=[file objectAtIndex:1];
NSString *video_title=[NSString stringWithFormat:@"Video%d.mov",buttonTag+1];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath=[NSString stringWithFormat:@"%@/%@",documentsDirectory,[appDelegate.array_Allideos objectAtIndex:buttonTag]];
NSData *videoData=[NSData dataWithContentsOfFile:filePath];
NSString *encodedFile=[videoData base64Encoding];
NSMutableDictionary *dictionary=[NSMutableDictionary new];
[dictionary setObject:@"xyz@gmail.com" forKey:@"UserID"];
[dictionary setObject:video_title forKey:@"video_title"];
[dictionary setObject:encodedFile forKey:@"video_stream"];
[dictionary setObject:date forKey:@"recorded_date"];
NSURL *url=[NSURL URLWithString:@"my url"];
NSError *error;
NSDictionary *jsonDict=[NSDictionary dictionaryWithDictionary:dictionary];
NSData *jsondata = [NSJSONSerialization dataWithJSONObject:jsonDict options:NSJSONWritingPrettyPrinted error:&error];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSLog(@"Length: %u",[jsondata length]);
//[request setValue:@"text/html,application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"multipart/form-data; boundary=AaB03x" /*@"image/png,charset=utf-8,application/x-www-form-urlencoded" */forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d", [jsondata length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:jsondata];
NSOperationQueue *queue = [NSOperationQueue currentQueue];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *err)
{
if ([data length] > 0 && err == nil){
NSError *parseError = nil;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
NSLog(@"Server Response (we want to see a 200 return code) %@",response);
NSLog(@"\nDictionary %@",dict);
//if response is generated sucessfully
}
else if ([data length] == 0 && err == nil){
NSLog(@"No data returned");
//no data, but tried
}
else if (err != nil)
{
NSLog(@"There was a error: %@",err);
//couldn't execute request
}
}];
}