如何在浏览器中显示文件

时间:2016-09-26 06:10:27

标签: java html angularjs spring rest

我正在尝试使用<iframe>在浏览器中显示文件(pdf,ms office files,txt)。我可以在iframe中显示txt和pdf文件,但无法对其他文件执行相同操作,会出现打开/保存对话框,而不是在iframe中显示它们。
我写了下面的代码,

 @RequestMapping(value="/getPreview",method=RequestMethod.GET)
@ResponseBody
public ResponseEntity<byte[]> getPreview(@RequestParam(value="file") String path) throws IOException{

     Path paths = Paths.get(path);
     byte[] data = Files.readAllBytes(paths);
     File file = new File(path);
     String name=file.getName();
     String mime=Files.probeContentType( Paths.get(path));
     HttpHeaders headers=new HttpHeaders();
     headers.setContentType(org.springframework.http.MediaType.parseMediaType(mime));
     headers.add("Content-Disposition", "inline;filename=" + name);
     headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
     ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(data,headers, HttpStatus.OK);

     return response;

}

我是否应该在响应标题中添加任何属性以使该要求有效?

HTML:<iframe src="/getPreview?file=filePath" style="width: 400px; height: 400px;"></iframe>

使用angualrjs作为前端。

0 个答案:

没有答案
相关问题