Uploading a large video from iphone to web server

2019-04-13 02:38发布

I'm trying to upload a large video from iphone to a web server that has php script.

I'm using NSInputStream to get file video chunks and I'm creating a request(POST) on each traversal of the

- (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.

        $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

1条回答
干净又极端
2楼-- · 2019-04-13 03:06

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.

查看更多
登录 后发表回答