我试图同时使用这个代码从我自己的服务器下载中心同时几个文件:未捕获的异常“NSFileHandleOperationException”
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// create
[[NSFileManager defaultManager] createFileAtPath:strFilePath contents:nil attributes:nil];
_file = [NSFileHandle fileHandleForUpdatingAtPath:strFilePath];// read more about file handle
if (_file) {
[_file seekToEndOfFile];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)receivedata
{
//write each data received
if(receivedata != nil){
if (_file) {
[_file seekToEndOfFile];
}
[_file writeData:receivedata];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
//close file after finish getting data;
[_file closeFile];
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
//do something when downloading failed
}
- (IBAction)downloadFromServer:(id)sender {
NSLog(@"File now being downloaded");
while (i<=3) {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSURL *strFileURL =[NSURL URLWithString:[NSString stringWithFormat:@"SomePath/pic%d.png", i]];
[request setURL:strFileURL];
NSURLConnection *conection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[conection start];
strFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic%d.png", i]];
}
}
我有3张图片,pic1.png,pic2.png和pic3.png。现在,如果我运行此代码,该应用程序将只保存一个名为pic3.png的损坏文件并自动崩溃。我需要下载所有三个文件,任何指向哪里我错了?
感谢您的回复......这是我第一次处理这种情况......您能帮我解决一个代码片段吗?非常感谢您的时间 – 2014-10-06 09:30:36
这是你的软件,你写的代码。 – gnasher729 2014-10-06 09:31:47
我不是要你给我写我的软件,我只是要求一个片段,可以澄清你的观点,以帮助我,因为它是我第一次在这些情况下 – 2014-10-06 09:33:21