尝试将视频文件发布到服务器,以字节为单位发布。在下面发布我用于发布的代码。
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ( [mediaType isEqualToString:@"public.movie" ])
{
if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0)
== kCFCompareEqualTo)
{
NSURL *videoUrl=(NSURL*)[info objectForKey:UIImagePickerControllerMediaURL];
NSData *webData = [NSData dataWithContentsOfURL:videoUrl];
[self post:webData];
if([profile isEqualToString:@"friend"])
{
[self callServerFriend];
}
if([profile isEqualToString:@"public"])
{
[self callServerPublic];
}
}
}
- (void)post:(NSData *)fileData
{
NSLog(@"POSTING");
index1 = [[NSUserDefaults standardUserDefaults] integerForKey:@"videoName"];
valIndex1 = index1;
NSString *str = [NSString stringWithFormat:@"video_%d.mp4",valIndex];
// Generate the postdata:
NSData *postData = [self generatePostDataForData: fileData];
profileImg = profilePic;
NSArray *arrImg = [profileImg componentsSeparatedByString:@"/"];
NSString *strImg1 = [arrImg objectAtIndex:[arrImg count]-2];
NSString *strImg2 = @"/";
NSString *strImg3 = [arrImg lastObject];
//NSLog(@"img2-----------------%@",strImg2);
NSString *pImg = [NSString stringWithFormat:@"%@%@%@",strImg1, strImg2, strImg3];
NSLog(@"img-----------------%@",pImg);
// Setup the request:
NSString *urlString = [[NSString stringWithFormat:@"http://www.myvnt.co/overheadPost.php?userId=%@&type=Video&text=%@&image=%@&location=%@&Upload=Upload", uid, statusPost, pImg, locc]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *wigiRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30.0];
[wigiRequest setHTTPMethod:@"POST"];
// just some random text that will never occur in the body
NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
// header value
NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",
stringBoundary];
// set header
[wigiRequest addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];
//add body
NSMutableData *postBody = [NSMutableData data];
NSLog(@"body made");
//IMAGE Posting
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
//"Content-Disposition: form-data; name=\"fileUpload\";filename=\"" + imagePath + "\"" + lineEn
//[postBody appendData:[@"Content-Disposition: form-data; name=\"fileUpload\"; filename=\"img.mp4\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"fileUpload\"; filename=\"%@\"\r\n", str]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/png\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
NSString *post = [NSString stringWithCString:"--AaB03x\r\nContent-Disposition: form-data; name=\"uploadFile\"; filename=\"img.mp4\"\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n\r\n" encoding:NSASCIIStringEncoding];
// Get the post header int ASCII format:
NSData *postHeaderData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
// Generate the mutable data variable:
postData = [[NSMutableData alloc] initWithLength:[postHeaderData length] ];
//[postData setData:postHeaderData];
// add it to body
[postBody appendData:postData];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// final boundary
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
// add body to post
[wigiRequest setHTTPBody:postBody];
NSLog(@"body set");
// pointers to some necessary objects
NSHTTPURLResponse* response =[[NSHTTPURLResponse alloc] init];
NSError* error = [[NSError alloc] init] ;
// synchronous filling of data from HTTP POST response
NSData *respData = [NSURLConnection sendSynchronousRequest:wigiRequest returningResponse:&response error:&error];
NSLog(@"just sent request");
NSString *responseHTML = [[NSString alloc] initWithBytes:[respData bytes]
length:[respData length]
encoding:NSUTF8StringEncoding];
NSLog(@"response dictionary : %@",responseHTML);
index1++;
[[NSUserDefaults standardUserDefaults] setInteger:index forKey:@"videoName"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (NSData *)generatePostDataForData:(NSData *)uploadData
{
// Generate the post header:
NSString *post = [NSString stringWithCString:"--AaB03x\r\nContent-Disposition: form-data; name=\"uploadFile\"; filename=\"img.mp4\"\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n\r\n" encoding:NSASCIIStringEncoding];
// Get the post header int ASCII format:
NSData *postHeaderData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
// Generate the mutable data variable:
NSMutableData *postData = [[NSMutableData alloc] initWithLength:[postHeaderData length] ];
[postData setData:postHeaderData];
// Add the image:
[postData appendData: uploadData];
// Add the closing boundry:
[postData appendData: [@"\r\n--AaB03x--" dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
// Return the post data:
return postData;
}
另一件事是它每次都发布具有相同名称的视频,因此每个新帖子都会替换以前的帖子。但主要问题是为什么它以字节形式发布我没有得到。请指导以上。提前致谢。