我正在将文件上传到服务器,使用NSStream API读取块。我正在尝试显示一个显示上传进度的进度条。我无法找到一个可靠的解决方案来确定读入文件的字节数。更改文件和块大小,我仍然可以获得总字节数,或者读取字节数为93或212的奇怪内容。有没有人有任何试图完成这个的提示?看起来应该很简单。
// create the write stream
NSOutputStream *outputStream = [NSOutputStream outputStreamToFileAtPath:uploadTempFilename append:NO];
[outputStream open];
const char *UTF8String;
size_t writeLength;
UTF8String = [multipartBegin UTF8String];
writeLength = strlen(UTF8String);
size_t __unused actualWrittenLength;
actualWrittenLength = [outputStream write:(uint8_t *)UTF8String maxLength:writeLength];
NSLog(@"actualWrittenLength1 %d bytes)", actualWrittenLength);//this will be 908
// open the input stream
const size_t bufferSize = 128 ;
size_t readSize = 0;
uint8_t *buffer = (uint8_t *)calloc(1, bufferSize);
[inImageStream open];
while ([inImageStream hasBytesAvailable]) {
if (!(readSize = [inImageStream read:buffer maxLength:bufferSize])) {
break;
}
size_t __unused actualWrittenLength;
actualWrittenLength = [outputStream write:buffer maxLength:readSize];
NSLog(@"actualWrittenLength2 %d bytes)", actualWrittenLength);//this always outputs 128, until the last call which will be something like 22
}
//NSLog(@"NSStreamFileCurrentOffsetKey %d", [outputStream valueForKey:NSStreamFileCurrentOffsetKey]);
[inImageStream close];
free(buffer);
UTF8String = [multipartEnd UTF8String];
writeLength = strlen(UTF8String);
actualWrittenLength = [outputStream write:(uint8_t *)UTF8String maxLength:writeLength];
NSLog(@"actualWrittenLength3 %d bytes)", actualWrittenLength);//this will always be 42
[outputStream close];
NSInputStream *inputStream = [NSInputStream inputStreamWithFileAtPath:uploadTempFilename];
[HTTPRequest setContentType:contentType];
return [HTTPRequest performMethod:@"POST" onURL:url withInputStream:inputStream knownContentSize:fileSize];
答案 0 :(得分:2)
您是否尝试过NSStreamFileCurrentOffsetKey
的属性值?
CF-API也值得一看。
如果需要,您可以继承-[NSOutputStream write:maxLength:]
来跟踪当前位置。