I am building an application to download the PDF file from out back-end server. I have written following code:
On Backend Server, following is the method:
@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;
}
I am calling this rest method from my adapter as:
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;
}
After the call Is finished I am getting following response from this service:
{
"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.
}
How can I parse this response to a PDF file and show it to the user? Also I am getting this response when I right click on adapter and choose run as "call mobile adapter", when I simply call this adapter method from the application using following code:
var invocationData = {
adapter : "MyAdapter",
procedure: "downloadFile",
parameters: []
};
WL.Client.invokeProcedure(invocationData, {
onSuccess: downloadFileOK,
onFailure: downloadFileFAIL,
onConnectionFailure: disconnectDetect
});
I am getting the same response on the browser's console but my "OnFailure" method "downloadFileFAIL" is getting called.
Edit Following is the log which is getting printed in Browser COnsole:
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
Edit2
Following are the links to the project and its resources:
- Java File
- PDF File
- MF Project