webView读取数据----乱码解决


读取磁盘中的data:

NSData *myData = [NSData dataWithContentsOfFile: fileDesPath];
NSString *ext = [m_openFiles.fileExtension lowercaseString];
NSString *mimeType;
if ([ext isEqualToString:@"doc"]) {
        mimeType = @"application/msword";
    }else if([ext isEqualToString:@"xls"]){
        mimeType = @"application/vnd.ms-excel";
    }else if([ext isEqualToString:@"wma"]){
        mimeType = @"audio/x-ms-wma";
    }else if([ext isEqualToString:@"jpg"] || [ext isEqualToString:@"jpeg"]){
        mimeType = @"image/jpeg";
    }else if([ext isEqualToString:@"tif"]||[ext isEqualToString:@"tiff"]){
        mimeType = @"image/tiff";
    }else if([ext isEqualToString:@"htm"]||[ext isEqualToString:@"html"]){
        mimeType = @"text/html";
    }else if([ext isEqualToString:@"mov"]){
        mimeType = @"video/quicktime";
    }else if([ext isEqualToString:@"mp4"]){
        mimeType = @"video/mp4";
    }else if([ext isEqualToString:@"txt"]){
        mimeType = @"text/plain";
    }else{
        mimeType = [NSString stringWithFormat:@"application/%@", ext];    //pdf
    }
    NSLog(mimeType);

//Here, I can get the myData. The value is the content of the doc file.
    if (myData) {  

        [self.myWebView loadData:myData MIMEType:mimeType textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:fileDesPath]];         
    }  

读取服务器返回的data:

 NSString *mimeType;
        if([fileType isEqualToString:@"doc"])
        {
            mimeType=@"application/msword";
        }
        else if([ fileType isEqualToString:@"xls"])
        {
            mimeType=@"application/vnd.ms-excel";
        }
        else if([fileType isEqualToString:@"wma"])
        {
            mimeType=@"audio/x-ms-wma";
        }
        else if([@"png" isEqualToString:fileType]||[@"jpg" isEqualToString:fileType]||[@"jpeg" isEqualToString:fileType]||[@"bmp" isEqualToString:fileType]||[@"gif" isEqualToString:fileType])
        {
            mimeType=@"image/jpeg";
        }
        else if([fileType isEqualToString:@"tif"]||[fileType isEqualToString:@"tiff"])
        {
            mimeType=@"image/tiff";
        }
        else if([fileType isEqualToString:@"htm"]||[fileType isEqualToString:@"html"])
        {
            mimeType=@"text/html";
        }
        else if([fileType isEqualToString:@"mov"])
        {
            mimeType=@"video/quicktime";
        }
        else if([fileType isEqualToString:@"mp4"])
        {
            mimeType=@"video/mp4";
        }
        else if([fileType isEqualToString:@"txt"]||[fileType isEqualToString:@"java"])
        {
            mimeType=@"text/plain";
        }
        else 
        {
            mimeType=[NSString stringWithFormat:@"application/%@",fileType];
        }
                                         //  (NSMutableData *)data 来自服务器端传递过来二进制流
        NSData *imgdata=[NSData dataWithData:data];
        
        if(imgdata){
               [textViewVc.webView loadData:imgdata MIMEType:mimeType textEncodingName:@"UTF_8" baseURL:nil];
        }         







你可能感兴趣的:(webView读取数据----乱码解决)