简体   繁体   English

iOS 5 - AFNetworking - 上传视频

[英]iOS 5 - AFNetworking - Uploading Video

For my app, users will be able to record a video or choose a video from their photo album and through a php script upload the video file to the server. 对于我的应用程序,用户将能够录制视频或从他们的相册中选择视频,并通过php脚本将视频文件上传到服务器。 To upload the file to the server, I am using AFNetworking. 要将文件上传到服务器,我正在使用AFNetworking。 I originally tried to upload the video from the photo album, but since I could not get that to work I added a video (that I know uploads fine through an html front I made for the php script) into the main bundle. 我最初试图从相册上传视频,但由于我无法上传,我在主套装中添加了一个视频(我知道通过我为php脚本制作的html前端上传得很好)。

The code is: 代码是:

NSString *vidURL = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mov"];
    NSData *videoData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:vidURL]];

    NSLog(@"The test vid's url is %@.",vidURL);

    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL fileURLWithPath: @"http://www.mywebsite.com"]];

    NSMutableURLRequest *afRequest = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload.php" parameters:nil constructingBodyWithBlock:^(id <AFMultipartFormData>formData) 
    {
        [formData appendPartWithFileData:videoData name:@"file" fileName:@"test.mov" mimeType:@"video/quicktime"]; 
    }];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:afRequest];

    [operation setUploadProgressBlock:^(NSInteger bytesWritten,long long totalBytesWritten,long long totalBytesExpectedToWrite) 
    {

        NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);

    }];

    [operation start];

My PHP script runs fine as I am able to upload the same video via an HTML form. 我的PHP脚本运行正常,因为我可以通过HTML表单上传相同的视频。 The above code fails to get to the setUploadProgressBlock, however. 但是,上面的代码无法访问setUploadProgressBlock。

Does anything stick out as broken to anyone or is there something else I am missing? 有什么东西对任何人来说都是破碎的,还是有什么我想念的东西?

Thanks in advance 提前致谢

This is what I used to fix the problem: 这是我用来解决问题的方法:

httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://www.mysite.com"]];

    NSMutableURLRequest *afRequest = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload.php" parameters:nil constructingBodyWithBlock:^(id <AFMultipartFormData>formData) 
    {
        [formData appendPartWithFileData:videoData name:@"file" fileName:@"filename.mov" mimeType:@"video/quicktime"];
    }];


    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:afRequest];

    [operation setUploadProgressBlock:^(NSInteger bytesWritten,long long totalBytesWritten,long long totalBytesExpectedToWrite) 
    {

        NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);

    }];

    [operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {NSLog(@"Success");} 
                                      failure:^(AFHTTPRequestOperation *operation, NSError *error) {NSLog(@"error: %@",  operation.responseString);}];
    [operation start];

Maybe the objects are getting deallocated immediately. 也许对象立即被释放。 Make the AFHTTPClient an instance variable of your class or subclass it and make it a singleton. 使AFHTTPClient成为您的类的实例变量或将其子类化并使其成为单例。

More Important: Replace this line: 更重要的是:替换此行:

[operation start];

with: 有:

[httpClient enqueueHTTPRequestOperation:operation];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM