iOS 判断文件下载的文件类型

来源:互联网 发布:plc控制电机正反转编程 编辑:程序博客网 时间:2024/05/18 00:54

1.把下载的文件放进数组中

2.在数据源方法中下:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return _cellDataArray.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *identifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil)

    {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

    }

    if (_cellDataArray)

    {

        NSString *filePath = _cellDataArray[indexPath.row];

        CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)[filePath pathExtension], NULL);

        CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass (UTI, kUTTagClassMIMEType);

        NSString *cbFileType = (__bridge NSString *)MIMEType;

        //图片.txt.doc.exel.ppt等类型判断

        if ([cbFileType isEqualToString:@"image/png"] || [cbFileType isEqualToString:@"image/jpeg"])

        {

            //cell图片显示

            cell.imageView.image = [UIImage imageNamed:@"000"];

        

        }else if([cbFileType isEqualToString:@"text/plain"] ||[cbFileType isEqualToString:@"application/vnd.ms-excel"] ||[cbFileType isEqualToString:@"application/vnd.ms-powerpoint"]||[cbFileType isEqualToString:@"application/msword"]||[cbFileType isEqualToString:@"application/vnd.openxmlformats-officedocument.wordprocessingml.document"])

        {

            //cell图片显示

            cell.imageView.image = [UIImage imageNamed:@"000"];

        }

        else

        {   //cell图片显示

            cell.imageView.image = [UIImage imageNamed:@"000"];

        }

        //所有的子文件名字

        cell.textLabel.text = _cellDataArray[indexPath.row];

      

    }

    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSString *filePath = _cellDataArray[indexPath.row];

    //类型判断

    CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)[filePath pathExtension], NULL);

    CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass (UTI, kUTTagClassMIMEType);

    //子控制器

    CBfile *fileController = [[CBfile alloc]init];

    fileController.cbFileType = (__bridge NSString *)MIMEType;

    NSString *abs = [[self.basepath stringByAppendingString:@"\/"] stringByAppendingString:_cellDataArray[indexPath.row]];

    fileController.nextAppearFilePath = abs;

    //push出来控制器

    [self.navigationController pushViewController:fileController animated:YES];

}

3.push出来的控制器里边判断读到的类型,依次调用自己的方法

- (void)viewDidLoad

{

    [super viewDidLoad];

        //png,jpg

    if ([self.cbFileType isEqualToString:@"image/png"]|| [self.cbFileType isEqualToString:@"image/jpeg"]) {

        [self appearImageView];

        //txt

    }else if([self.cbFileType isEqualToString:@"text/plain"]) {

        [self appearWebView];

        //word

    }else if([self.cbFileType isEqualToString:@"application/msword"]||[self.cbFileType isEqualToString:@"application/vnd.openxmlformats-officedocument.wordprocessingml.document"]) {

        [self appearLableView];

        //exel

    }else if([self.cbFileType isEqualToString:@"application/vnd.ms-excel"]) {

        [self appearExel];

        //ppt

    }else if([self.cbFileType isEqualToString:@"application/vnd.ms-powerpoint"]) {

        [self appearPpt];

        //other

    }else {

        [self appearOther];

        

    }

}



1 0
原创粉丝点击