我正在构建一个从后端服务器下载PDF文件的应用程序。我已经写了下面的代码:通过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;
}
我从我的适配器调用此休息方法为:
在通话结束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文件并显示给用户?我也正在此回应时,我在适配器右击并选择运行“召移动适配器”,当时我只是从应用程序中使用下面的代码调用该适配器方法:
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
EDIT2
以下是链接到项目及其资源:
你在哪里测试这一个额外的依赖?桌面浏览器还是设备?不要指望它的工作原理一样!浏览器与设备不一样。在设备中,您必须处理Cordova的响应,以访问设备的文件系统,或将响应存储在JSONStore - >您要在哪里存储响应? HTML5本地存储?它只有5mb的容量...使用JSONStore来代替。 –
我正在使用桌面浏览器。但还有一件事是,即使答案没有问题,成功的方法也不会被调用。 –
“statusCode”:20和“statusReason”:“OK”...我错过了什么吗?你能指出我错误吗? –