从iPhone上传一个大的视频到Web服务器 [英] Uploading a large video from iphone to web server

查看:188
本文介绍了从iPhone上传一个大的视频到Web服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用 NSInputStream 来获取文件视频块,我在每次遍历

 时创建一个请求(POST) - (void)流:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode 

方法读数据作为参数传递。



这里是我用来获取数据块的代码

   - (无效)流:(NSStream *)流的handleEvent:(NSStreamEvent)EVENTCODE 
{
开关(EVENTCODE)
{
情况下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.phpparams:params httpMethod:@POST];

[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 = nil;
break;





它将数据发送给服务器,我能够写入文件块。但是,问题是,如果有多个块,文件将被损坏,我无法打开视频文件。



我检查了服务器和客户端上的文件大小,两者完全相同。



以下是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);

我在这做错了什么?请帮忙!

我被困在这个问题!!

在此先感谢,



Suraj

解决方案

实际上,我是在同一时间发送不同的视频块。问题出现了,因为后来的视频块在第一块视频之前到达了服务器。

我解决了这个问题,只有在第一个数据块到达web服务器并在客户端获得响应后才发送第二个数据块。 $ b

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

解决方案

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.

这篇关于从iPhone上传一个大的视频到Web服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆