java – 在GWT中使用RequestBuilder处理附件
我正在从GWT Client向HTTPServlet发出HTTP POST请求.该Servlet正在从请求内容创建一个PDF文件,并将其写入响应流.
回应流的标题是: Content-Disposition: attachment; filename=report.pdf 我想在用户浏览器的新窗口中打开此PDF,或者提示他下载它. import com.google.gwt.http.client.*; ... String url = "http://www.myserver.com/getData?type=3"; RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,URL.encode(url)); try { Request request = builder.sendRequest(data,new RequestCallback() { public void onError(Request request,Throwable exception) { // Couldn't connect to server (could be timeout,SOP violation,etc.) } public void onResponseReceived(Request request,Response response) { if (200 == response.getStatusCode()) { // Process the response in response.getText() // Window.open(url,"_blank",""); } else { // Handle the error. Can get the status text from response.getStatusText() } } }); } catch (RequestException e) { // Couldn't connect to server } 应该如何处理onResponseRecieved的响应? 解决方法
我认为在这种情况下,你不应该使用单一的RequestBuilder AJAX调用.您可以通过调用正常调用来依赖默认的浏览器行为,并让浏览器处理PDF响应(用PDF查看器插件显示或打开保存对话框).
实现这一点有几种选择: >如果您可以通过GET请求传递数据(仅适用于小数据卷),则可以使用数据作为GET参数创建URL,然后打开一个新的浏览器窗口,其中Window.open()将URL传递给数据. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |