通过 MobileFirst Adapter 下载 PDF 文件
Download PDF file from through MobileFirst Adapter
我正在构建一个应用程序以从后端服务器下载 PDF 文件。我写了以下代码:
在后端服务器上,方法如下:
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces("application/pdf")
public Response download() {
ResponseBuilder response = Response.ok((Object) new File("myFile.pdf"));
response.header("Content-Disposition", "attachment; filename=myFile.pdf");
Response responseBuilder = response.build();
return responseBuilder;
}
我正在从我的适配器中调用此 rest 方法:
function downloadFile(){
var input = {
method : 'post',
returnedContentType : "plain",
path : "getfiles",
body : {
contentType : 'application/json;charset=utf-8',
content : JSON.stringify({username: "testuser"})
}
};
var response = WL.Server.invokeHttp(input);
return response;
}
通话结束后,我收到此服务的以下响应:
{
"errors": [
],
"info": [
],
"isSuccessful": true,
"responseHeaders": {
"Content-Disposition": "attachment; filename=myFile.pdf",
"Content-Length": "692204",
"Content-Type": "application\/pdf",
"Date": "Thu, 15 Oct 2015 15:19:56 GMT",
"X-Powered-By": "Servlet\/3.0"
},
"responseTime": 11,
"statusCode": 200,
"statusReason": "OK",
"text":"%PDF-1.6\n%����\n159 0 obj\n<<\/Linearized 1\/L 692204\/O 162\/E 156949\/N 25\/T 691602\/H [ 531 579]>>\nendobj\n"
--long lines of characters in text field.
}
如何将此响应解析为 PDF 文件并将其显示给用户?当我右键单击适配器并选择 运行 作为 "call mobile adapter" 时,我也得到了这个响应,当我使用以下代码从应用程序中简单地调用这个适配器方法时:
var invocationData = {
adapter : "MyAdapter",
procedure: "downloadFile",
parameters: []
};
WL.Client.invokeProcedure(invocationData, {
onSuccess: downloadFileOK,
onFailure: downloadFileFAIL,
onConnectionFailure: disconnectDetect
});
我在浏览器的控制台上得到了相同的响应,但是我的 "OnFailure" 方法 "downloadFileFAIL" 被调用了。
编辑
以下是在浏览器控制台中打印的日志:
R\n>>\nstartxref\n451945\n%%EOF","errors":[],"isSuccessful":true,"statusReason":"OK","responseHeaders":{"Date":"Thu, 15 Oct 2015 21:52:40 GMT","Content-Length":"453132","Content-Disposition":"attachment; filename=myFile.pdf","Content-Type":"application\/pdf","X-Powered-By":"Servlet\/3.0"},"warnings":[],"responseTime":15,"totalTime":151,"info":[]}
worklight.js:5356 Procedure invocation error.WL.Logger.__log @ worklight.js:5356
worklight.js:5360 Uncaught Exception: Uncaught SyntaxError: Unexpected number at (compiled_code):3879WL.Logger.__log @ worklight.js:5360
worklight.js:3879 Uncaught SyntaxError: Unexpected number
worklight.js:5992 Local storage capacity reached. WL.Logger will delete old logs to make room for new ones.
worklight.js:5356 Piggybacking event transmission
worklight.js:5356 Flush called
编辑2
以下是项目及其资源的链接:
更新:
您遇到的问题是因为JS无法处理二进制数据。最好的选择是在后端服务器上对文件进行 base64 编码,然后在保存到文件之前在应用程序上对文件进行 base64 解码。例如:
后端服务器:
您的项目需要额外的依赖项import org.apache.commons.codec.binary.Base64;
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces("application/pdf")
public Response downloads() throws IOException {
File file = new File("myFile.pdf");
InputStream fileStream = new FileInputStream(file);
byte[] data = new byte[1024];
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int read = 0;
while ((read = fileStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, read);
}
buffer.flush();
fileStream.close();
ResponseBuilder response = Response.ok(Base64.encodeBase64(buffer.toByteArray()));
response.header("Content-Disposition", "attachment; filename=myFile.pdf");
Response responseBuilder = response.build();
return responseBuilder;
}
我正在构建一个应用程序以从后端服务器下载 PDF 文件。我写了以下代码:
在后端服务器上,方法如下:
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces("application/pdf")
public Response download() {
ResponseBuilder response = Response.ok((Object) new File("myFile.pdf"));
response.header("Content-Disposition", "attachment; filename=myFile.pdf");
Response responseBuilder = response.build();
return responseBuilder;
}
我正在从我的适配器中调用此 rest 方法:
function downloadFile(){
var input = {
method : 'post',
returnedContentType : "plain",
path : "getfiles",
body : {
contentType : 'application/json;charset=utf-8',
content : JSON.stringify({username: "testuser"})
}
};
var response = WL.Server.invokeHttp(input);
return response;
}
通话结束后,我收到此服务的以下响应:
{
"errors": [
],
"info": [
],
"isSuccessful": true,
"responseHeaders": {
"Content-Disposition": "attachment; filename=myFile.pdf",
"Content-Length": "692204",
"Content-Type": "application\/pdf",
"Date": "Thu, 15 Oct 2015 15:19:56 GMT",
"X-Powered-By": "Servlet\/3.0"
},
"responseTime": 11,
"statusCode": 200,
"statusReason": "OK",
"text":"%PDF-1.6\n%����\n159 0 obj\n<<\/Linearized 1\/L 692204\/O 162\/E 156949\/N 25\/T 691602\/H [ 531 579]>>\nendobj\n"
--long lines of characters in text field.
}
如何将此响应解析为 PDF 文件并将其显示给用户?当我右键单击适配器并选择 运行 作为 "call mobile adapter" 时,我也得到了这个响应,当我使用以下代码从应用程序中简单地调用这个适配器方法时:
var invocationData = {
adapter : "MyAdapter",
procedure: "downloadFile",
parameters: []
};
WL.Client.invokeProcedure(invocationData, {
onSuccess: downloadFileOK,
onFailure: downloadFileFAIL,
onConnectionFailure: disconnectDetect
});
我在浏览器的控制台上得到了相同的响应,但是我的 "OnFailure" 方法 "downloadFileFAIL" 被调用了。
编辑 以下是在浏览器控制台中打印的日志:
R\n>>\nstartxref\n451945\n%%EOF","errors":[],"isSuccessful":true,"statusReason":"OK","responseHeaders":{"Date":"Thu, 15 Oct 2015 21:52:40 GMT","Content-Length":"453132","Content-Disposition":"attachment; filename=myFile.pdf","Content-Type":"application\/pdf","X-Powered-By":"Servlet\/3.0"},"warnings":[],"responseTime":15,"totalTime":151,"info":[]}
worklight.js:5356 Procedure invocation error.WL.Logger.__log @ worklight.js:5356
worklight.js:5360 Uncaught Exception: Uncaught SyntaxError: Unexpected number at (compiled_code):3879WL.Logger.__log @ worklight.js:5360
worklight.js:3879 Uncaught SyntaxError: Unexpected number
worklight.js:5992 Local storage capacity reached. WL.Logger will delete old logs to make room for new ones.
worklight.js:5356 Piggybacking event transmission
worklight.js:5356 Flush called
编辑2
以下是项目及其资源的链接:
更新:
您遇到的问题是因为JS无法处理二进制数据。最好的选择是在后端服务器上对文件进行 base64 编码,然后在保存到文件之前在应用程序上对文件进行 base64 解码。例如:
后端服务器:
您的项目需要额外的依赖项import org.apache.commons.codec.binary.Base64;
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces("application/pdf")
public Response downloads() throws IOException {
File file = new File("myFile.pdf");
InputStream fileStream = new FileInputStream(file);
byte[] data = new byte[1024];
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int read = 0;
while ((read = fileStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, read);
}
buffer.flush();
fileStream.close();
ResponseBuilder response = Response.ok(Base64.encodeBase64(buffer.toByteArray()));
response.header("Content-Disposition", "attachment; filename=myFile.pdf");
Response responseBuilder = response.build();
return responseBuilder;
}