gpt4 book ai didi

iphone - 从 iPhone 上传视频文件到服务器

转载 作者:可可西里 更新时间:2023-11-01 06:23:37 25 4
gpt4 key购买 nike

我知道如何将图片上传到运行 PHP 的服务器,但我一直坚持上传视频。

我用过this advice上传我的视频文件。

发帖方式都可以。我在服务器上得到的是一个0字节的文件。

我的代码如下:

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {

NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSData *videoData = [NSData dataWithContentsOfFile:[videoURL path]];
}

此视频数据在我的 POST 方法中传递。

我应该怎么做?

最佳答案

要发布视频,您需要在图像选择器委托(delegate)后使用此功能

- (NSData *)generatePostDataForData:(NSData *)uploadData
{
// Generate the post header:
NSString *post = [NSString stringWithCString:"--AaB03x\r\nContent-Disposition: form-data; name=\"upload[file]\"; filename=\"somefile\"\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;
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

//assign the mediatype to a string
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

//check the media type string so we can determine if its a video
if ([mediaType isEqualToString:@"public.movie"]){
NSLog(@"got a movie");
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSData *webData = [NSData dataWithContentsOfURL:videoURL];
[self post:webData];
[webData release];

}

发布视频使用此功能

- (void)post:(NSData *)fileData
{

NSLog(@"POSTING");

// Generate the postdata:
NSData *postData = [self generatePostDataForData: fileData];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

// Setup the request:
NSMutableURLRequest *uploadRequest = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.example.com:3000/"] cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval: 30 ] autorelease];
[uploadRequest setHTTPMethod:@"POST"];
[uploadRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[uploadRequest setValue:@"multipart/form-data; boundary=AaB03x" forHTTPHeaderField:@"Content-Type"];
[uploadRequest setHTTPBody:postData];

// Execute the reqest:
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:uploadRequest delegate:self];
if (conn)
{
// Connection succeeded (even if a 404 or other non-200 range was returned).
NSLog(@"sucess");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Got Server Response" message:@"Success" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else
{
// Connection failed (cannot reach server).
NSLog(@"fail");
}

}

关于iphone - 从 iPhone 上传视频文件到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12245929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com