欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > HTML >内容正文

HTML

在浏览器中直接打开PDF

发布时间:2024/1/18 HTML 35 豆豆
生活随笔 收集整理的这篇文章主要介绍了 在浏览器中直接打开PDF 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

如何在浏览器中直接打开pdf/doc/excel文件

我在文件下载的部分 需要在具体文件名上加超链接 到可以直接在浏览器打开
另外下载时候 能不能也默认直接打开在浏览器 而不是显示对话框
 
请做个类似功能的JE友给个帮助 谢谢先

在struts2 + spring + hibernate中
问题补充:

引用
response.setHeader("Content-Disposition", "inline"+ "; filename=/"" + encodedShowName+"/""); 



这个我试过 会跳出对话框放你选择 是否打开或者保持

我想要的是直接打开在浏览器

谢谢
问题补充:
我是这么写的

Xml代码  
  • < action   name = "downloadKnowledge"   class = "knowledgeAction"   method = "downloadKnowledge" >   
  •     <!-- 指定被下载资源的位置 -->    
  •        < param   name = "inputPath" > /upload/ </ param >    
  •          
  •     < result   name = "success"   type = "stream" >   
  •         < param   name = "contentType" > ${contentType} </ param >   
  •         < param   name = "inputName" > targetFile </ param >   
  •         < param   name = "contentDisposition" > ${filename} </ param >   
  •         < param   name = "bufferSize" > 4096 </ param >   
  •     </ result >   
  • </ action >   


  • java代码

    Java代码  
  • /**  
  •  * 下载Action  
  •  * @return  
  •  */   
  • public  String downloadKnowledge()  throws  Exception{  
  •     checkFormat(download);  
  •     return  SUCCESS;  
  • }  
  •   
  • /**  
  •  * 检查格式,文件后缀  
  •  * @param fileName  
  •  */   
  • private   void  checkFormat(String fileName){  
  •       
  •     String[] splitString = fileName.split("//." );  
  •     int  length = splitString.length;  
  •     String format = null ;  
  •     if  (length >  1 ){  
  •         format = splitString[length - 1 ];  
  •     }  
  •       
  •     DataFormat df = Enum.valueOf(DataFormat.class , format);  
  •     switch  (df){  
  •         case  xls:  
  •             // contentType设定       
  •             contentType = "application/vnd.ms-excel;charset=utf-8" ;      
  •             // attachment表示网页会出现保存、打开对话框       
  •             filename = "inline; filename="  + fileName;      
  •             break ;                
  •         case  xlsx:  
  •             // contentType设定       
  •             contentType = "application/vnd.ms-excel;charset=utf-8" ;      
  •             // attachment表示网页会出现保存、打开对话框       
  •             filename = "inline; filename="  + fileName;      
  •             break ;  
  •         case  pdf:    
  •             // contentType设定       
  •             contentType = "application/pdf;charset=utf-8" ;      
  •             // attachment表示网页会出现保存、打开对话框       
  •             filename = "inline; filename="  + fileName;   
  •             break ;  
  •         case  doc:  
  •             // contentType设定       
  •             contentType = "application/msword;charset=utf-8" ;      
  •             // attachment表示网页会出现保存、打开对话框       
  •             filename = "inline; filename="  + fileName;      
  •             break ;                
  •         case  docx:  
  •             // contentType设定       
  •             contentType = "application/msword;charset=utf-8" ;      
  •             // attachment表示网页会出现保存、打开对话框       
  •             filename = "inline; filename="  + fileName;      
  •             break ;  
  •         case  txt:  
  •             // contentType设定       
  •             contentType = "text/plain;charset=utf-8" ;      
  •             // attachment表示网页会出现保存、打开对话框       
  •             filename = "inline; filename="  + fileName;      
  •             break ;  
  •         default :  
  •             // contentType设定       
  •             contentType = "text/plain;charset=utf-8" ;      
  •             // attachment表示网页会出现保存、打开对话框       
  •             filename = "inline; filename="  + fileName;    
  •             break ;  
  •     }     

  • 总结

    以上是生活随笔为你收集整理的在浏览器中直接打开PDF的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。