简体   繁体   English

从iPhone将大型视频上传到Web服务器

[英]Uploading a large video from iphone to web server

I'm trying to upload a large video from iphone to a web server that has php script. 我正在尝试将大型视频从iPhone上传到具有php脚本的Web服务器。

I'm using NSInputStream to get file video chunks and I'm creating a request(POST) on each traversal of the 我使用NSInputStream来获取文件视频块,并在每次遍历时创建一个request(POST)

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode

method, with the read data passed as parameter. 方法,将读取的数据作为参数传递。

Here is the code I'm using to get chunks of data 这是我用来获取数据块的代码

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode 
{
switch(eventCode) 
{
    case NSStreamEventHasBytesAvailable:
    {
        NSMutableData *dataSlice;

        uint8_t buf[1048576];
        unsigned int len = 0;
        len = [(NSInputStream *)stream read:buf maxLength:1048576];
        if(len) 
        {
            dataSlice = [NSMutableData dataWithBytes:(const void *)buf length:len];


            NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:folderNameForUpload, kFolderName,
                                           @"abcd.MOV", kFileName,
                                           @"MOV", kFileType,
                                           nil];
            MKNetworkOperation *op = [self.networkEngine operationWithPath:@"upload.php" params:params httpMethod:@"POST"];

            [op addData:dataSlice forKey: @"file"
                             mimeType: @"image/mov"
                             fileName: @"abcd"];

            [op onCompletion:^(MKNetworkOperation *completedOperation) {

            } onError:^(NSError *error) {

            }];

            [[WebRequest sharedInstance].networkEngine enqueueOperation: op];


        }
        else 
        {
            NSLog(@"NO MORE BUFFER!");
        }
        break;
    }


    case NSStreamEventEndEncountered:
    {
        [stream close];
        [stream removeFromRunLoop:[NSRunLoop currentRunLoop]
                          forMode:NSDefaultRunLoopMode];
        [stream release];
        stream = nil;
        break;
    }
}
}

It is sending the data to the server, and I'm able to write the chunks into a file. 它正在将数据发送到服务器,并且我能够将这些块写入文件中。 But, the problem is that, if there are more than one chunk, the file will become corrupted and I'm not able to open the video file. 但是,问题在于,如果有多个块,文件将被损坏,而我无法打开视频文件。

I checked the file size on both server and client, and both are exactly same. 我检查了服务器和客户端上的文件大小,两者完全相同。

Below is the php script, I'm using to merge video file chunks. 以下是php脚本,我用于合并视频文件块。

        $tmp_file = $_FILES['file']['tmp_name'];

        $write_handle = fopen($fileURL, "ab+");
        $read_handle = fopen($tmp_file, "rb");

        $contents = fread($read_handle, filesize($tmp_file));
        fwrite($write_handle, $contents);

        fclose($write_handle);
        fclose($read_handle);

What Am I doing wrong here?, Please help! 我在这里做错了什么?,请帮忙!

I'm stuck over this problem!! 我被这个问题困住了!!

Thanks in Advance, 提前致谢,

Suraj 苏拉杰

I got the problem myself guys. 我自己遇到了问题。 Actually, I was sending different chunks of video at the same time. 实际上,我同时发送了不同的视频块。 And the problem was arising because the later chunks of video reached server before the first chunk of video. 出现问题的原因是,随后的视频块在第一部分视频之前到达服务器。

I solved the problem by sending second chunk of video only after the first chunk is reached web server and the response is got at the client side. 我仅在第一个视频块到达Web服务器并在客户端获得响应后才发送第二个视频块,从而解决了该问题。

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

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