项目中最近添加了新功能,从第三方app 分享文件过来,并展示出来
相关MIMEType如下
mimetypes | mimes |
---|---|
image/jpeg | jpg |
image/jpeg | jpeg |
image/png | png |
image/webp | webp |
application/vnd.ms-excel | xls |
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet | xlsx |
application/msword | doc |
application/vnd.openxmlformats-officedocument.wordprocessingml.document | docx |
application/vnd.openxmlformats-officedocument.presentationml.presentation | pptx |
application/vnd.ms-powerpoint | ppt |
application/pdf | |
text/plain | txt |
代码实现
NSLog(@"filpath------%@", filpath);
NSData *Data = [NSData dataWithContentsOfFile:_filePath];
NSString *type = [filpath componentsSeparatedByString:@"."].lastObject;
if ([type isEqualToString:@"docx"])
{
[self.webView loadData:Data MIMEType:@"application/vnd.openxmlformats-officedocument.wordprocessingml.document" textEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
}else if ([type isEqualToString:@"doc"])
{
[self.webView loadData:Data MIMEType:@"application/msword" textEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
}else if([type isEqualToString:@"txt"])
{
[self.webView loadData:Data MIMEType:@"text/plain" textEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
}else if([type isEqualToString:@"PDF"] || [type isEqualToString:@"pdf"])
{
[self.webView loadData:Data MIMEType:@"application/pdf" textEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
}else if([type isEqualToString:@"PNG"] || [type isEqualToString:@"png"])
{
[self.webView loadData:Data MIMEType:@"image/png" textEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
}else if([type isEqualToString:@"ppt"])
{
[self.webView loadData:Data MIMEType:@"application/vnd.ms-powerpoint" textEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
}else if ([type isEqualToString:@"pptx"])
{
[self.webView loadData:Data MIMEType:@"application/vnd.openxmlformats-officedocument.presentationml.presentation" textEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
}else if ([type isEqualToString:@"xlsx"])
{
[self.webView loadData:Data MIMEType:@"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" textEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
}else if ([type isEqualToString:@"xls"])
{
[self.webView loadData:Data MIMEType:@"application/vnd.ms-excel" textEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
}
else{
}