我正在尝试将大型视频从 iphone 上传到具有 php 脚本的网络服务器。
我NSInputStream
用来获取文件视频块,并且在每次遍历
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode
方法,将读取的数据作为参数传递。
这是我用来获取数据块的代码
- (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;
}
}
}
它将数据发送到服务器,我可以将块写入文件。但是,问题是,如果有超过一个块,文件将被损坏,我无法打开视频文件。
我检查了服务器和客户端上的文件大小,两者完全相同。
下面是 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);
我在这里做错了什么?,请帮忙!
我被这个问题困住了!!
提前致谢,
苏拉杰