简体   繁体   English

SmartGWT〜DSRequest下载文件

[英]SmartGWT ~ DSRequest download file

I am writing a server side class in smartGWT that fetches a record from a table and downloads a binary blob as a file from the database. 我正在smartGWT中编写服务器端类,该类从表中获取记录,并从数据库中下载二进制blob作为文件。 The goal is to get the binary file attachment to download to local computer. 目的是获取二进制文件附件以下载到本地计算机。

I was told that I could use DSRequest.getUploadedFile(attachment) to download the file but looking into it, it looks as though this method does not get an uploaded file from the database to download but gets a file to upload to the database. 有人告诉我,我可以使用DSRequest.getUploadedFile(attachment)下载文件,但进行调查,似乎该方法未从数据库中获取要上传的文件,而是将文件上传至了数据库。

Is there a way that I can get a file from the database and return it to the user as a download in server side code? 有没有一种方法可以从数据库中获取文件并将其作为服务器端代码中的下载返回给用户? We know we can do this in client side code but we would like to be able to do it on the server. 我们知道我们可以在客户端代码中做到这一点,但我们希望能够在服务器上做到这一点。

Here is how I am getting the record that contains the file I want to send to the user. 这是我获取包含要发送给用户的文件的记录的方式。

DSRequest dsReq = new DSRequest("table", "fetch");
dsReq.setCriteria("criteria", processFlowData.getVariableMap().get("critera"));

DataSource ds = dsReq.getDataSource();
DSResponse dsResp = ds.execute(dsReq);

if (dsResp.getStatus() < 0) {
    //Handle Errors
} else {
    if (!dsResp.getRecord().isEmpty()) {
        //Download File Here
    }
}

I am using Eclipse Kepler, SmartGWT, Java EE. 我正在使用Eclipse Kepler,SmartGWT,Java EE。

you could do something like this: 您可以执行以下操作:

public static void downloadFile(DSRequest dsRequest, RPCManager rpcManager, HttpServletResponse servletResponse) throws Exception {
    rpcManager.doCustomResponse();
    ServletOutputStream outputStream = servletResponse.getOutputStream();
    servletResponse.addHeader("Content-disposition", "attachment; filename=test.pdf");
    servletResponse.setContentType("application/x-www-form-urlencoded");
    OutputStream responseOutputStream = servletResponse.getOutputStream();
    // write file to responseOutputStream
    responseOutputStream.flush();
    responseOutputStream.close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM