我有一个pdf存储在我的本地和我的控制器方法我试图以ResponseEntity的形式返回它 如果我直接显示来自控制器的响应它工作正常,但我要求它将pdf嵌入到IE上的一段jsp上,这是无效的。
以下是代码:
控制器:
@RequestMapping(value="/cde")
public ResponseEntity<byte[]> getPDF() {
// json => emp
System.out.println("here");
byte[] contents=null;
// generate the file
try{
Path path = Paths.get("C:\\abc.pdf");
contents = Files.readAllBytes(path);
System.out.println("CONTENTS"+contents);
}
catch(Exception e){
e.printStackTrace();
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/pdf"));
String filename = "output.pdf";
headers.setContentDispositionFormData(filename, filename);
headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(contents, headers, HttpStatus.OK);
return response;
}
JSP:
<a href="#" id="cde">Fetch pdf</a>
<div id="doc2"></div>
<script>
$( "#cde" ).click(function() {
alert('here');
$.ajax({
type : "GET",
url : 'cde',
contentType: "application/json; charset=utf-8",
data: ""
,
error: function(XMLHttpRequest, error, errorThrown){
alert(error);
alert(errorThrown);
},
success : function(data) {
alert('success');
alert(data);
var obj = $('<object type="application/pdf" width="100%" height="100%" border="2"></object>');
obj.attr('data','data:application/pdf;base64,'+data);
$("#doc2").append(obj);
}
});
});
</script>
代码正在使用FIREFOX,但不适用于IE