如何使用RESTful/ajax/js在UI上显示PDF,使用来自UI的表单输入
How to show PDF on UI using RESTful/ajax/js using form input from UI?
我正在尝试使用javascript和ajax显示RESTful服务UI的PDF报告。它可以很好地处理单个输入,但对于使用UI中的表单的多个输入,我后来将其更改为JSON对象,它没有以正确的格式显示PDF。我已经使用POSTMAN检查了服务,它运行良好(下载PDF)。
下面是用JAVA编写的服务代码。
@POST
@Path("/CustomizedReport")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces("application/pdf")
public Response getCustomizedReport(SubscriberCriteriaTool sct) throws ParseException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
new CustomizedReport().aaGeneratePdf(outputStream, sct);
byte[] bytes = outputStream.toByteArray();
DataSource dataSource = new ByteArrayDataSource(bytes, "application/pdf");
ResponseBuilder response = Response.ok(dataSource);
response.header("Content-Disposition", "inline; filename=CustomizedReport.pdf");
return response.build();
}
下面是javascript中的函数。
function generateCustomizedReport() {
console.log('generateCustomizedReport');
$.ajax({
type : 'POST',
contentType : 'application/json',
url : 'http://localhost:8080/Morpheus/api/reports/CustomizedReport',
data : formToJSONReport(),
success : function(response) {
document.write(response);
},
error : function(error, status) {
window.alert("Problem retrieving PDF.'nThe error status is: " + error);
}
});
}
下面是将表单更改为JSON的函数。现在它是硬编码的,但计划在工作后从UI中获取输入。这与我在POSTMAN上检查的字符串完全相同,它给出了正确的结果(下载PDF)。
function formToJSONReport() {
return JSON.stringify({
"fromDate" : "10/01/2014",
"rateCenter" : "HUNTINGTON",
"status" : "SUCCESSFUL",
"toDate" : "11/01/2014",
"group" : "Third"
});
}
这是我在UI上得到的PDF文本(无法发布快照,因为我是stackoverflow的新手)。
%PDF-1.4%����1 0 obj<>流动����ExifII*��Ducky7���http://ns.adobe.com/xap/1.0/��!Adobed�B�M@D]��� ##########
提前感谢:-)
您的响应是文件流字节,我们无法通过将字节直接写入文档来显示文件,但我们可以使用iframe或新窗口下载具有指定内容类型的流(此处为application/pdf)(或另一个技术人员通过以iframe为目标或_blank附加表单,即响应将在新窗口新窗口中打开)。
尝试将您的成功函数更改为以下
success : function(response) {
//This double pass request Technic is to download the file stream via iframe.
$("<iframe>").attr("src", 'http://localhost:8080/Morpheus/api/reports/CustomizedReport')
//.hide()//hide it while download
.appendTo("body");
//OR you can try with popup window
//window.open('http://localhost:8080/Morpheus/api/reports/CustomizedReport')
}
通常我们在控制器中写两个动作
- 对于Validate Request并返回带有有效令牌的响应(某些密钥和时间戳的加密值)
- 通过iframe(或新窗口)获取文件,该文件具有在第一个请求成功(或完成)回调的响应中接收的上述有效令牌
相关文章:
- 角度ui选择标记模糊时丢失文本输入
- Jquery UI日期选择器不关注输入
- 如何在angular bootstrap ui中聚焦活动导航选项卡中的拳头输入字段
- 如何在状态输入ui路由器时立即显示模板,然后当承诺被解决时显示数据
- 为什么Jquery UI会擦除我的输入框
- 如何启用由日期选择器 ui 附加的输入?(JQuery 插件)
- 使用jquery UI next按钮提交输入字段值,php将返回结果
- 将“日期”设置为Jquery UI日期选择器输入文本框
- 如何使用ian:accounts-ui-botstrap-3根据Meteor中的用户输入设置配置文件字段值
- 具有输入类型文件字段的语义UI重置表单
- jQuery UI Spinner与输入类型=“;数字”;
- Jquery UI选项卡-根据输入字段值,使用“下一步”/“上一步”按钮推进选项卡
- 将输入id传递到jquery ui自动完成中的url
- jquery ui:检测输入中的首次时间变化
- 在jQuery UI日期选择器的输入中显示完整日期
- Kendo ui Grid:标准的HTML5输入日期时间可以用作单元格编辑器吗
- 角度 UI 预测文本:如何输入未包含在输入数组中的数据
- 如何单元测试输入和解析 UI 路由器状态
- 剑道UI输入焦点-在选择和Ajax调用之后,下拉列表保留焦点
- jQuery UI输入自动完成无匿名功能