PDF下载不使用ext js工作

时间:2013-11-26 07:31:09

标签: ajax spring-mvc pdf extjs

我是Extjs的新手。我试图通过单击网格表的PDF列中的pdf链接来下载来自服务器的pdf文件,但无法下载该文件。请帮我解决这个问题。

请在下面找到我的代码段。

PDF专栏

{
    header: "PDF",
    align: 'center',
    width: 35,
    renderer: function (value, metaData, record, rowIndex, colIndex, store) {
        if(record.get("extension") == "pdf") {
            metaData.style = 'text-decoration:underline;';
            metaData.tdAttr = 'data-qtip="' + record.get("eventURI") + '"';
            return 'PDF';

        }
        else
            return '';

    }
}

AJAX Call

if (colName == 'PDF'  && currentRow.get('extension')=="pdf") {
    if (!Ext.fly('fsfrmDummy')) {
        var frm = document.createElement('form');
        frm.id = 'fsfrmDummy';
        frm.name = 'fsfrmDummy';
        frm.className = 'x-hidden';
        document.body.appendChild(frm);
    }
    Ext.Ajax.request({
        method: 'post',
        url: 'edash/downloadDoc.action',
        params: {docUri: currentRow.get('eventURI')},
        success: function (response, config) {
        },
        failure: function (response, config) {
            appException('ERROR', response.message,
                    Ext.MessageBox.ERROR,
                    Ext.Msg.OK);
        },
        form: Ext.fly('fsfrmDummy'),
        isUpload: true
    });
}

事件URI

{
    header: "EVENT_URI",
    width: 95,
    sortable: true,
    dataIndex: 'eventURI',

    renderer: function (value, metaData,record, rowIndex, colIndex, store) {


        if(record.get("extension") == "pdf") { 
            metaData.style = 'text-decoration:underline;';

        }
        return value;

    }

}

服务器端代码

public @ResponseBody
Map<String, ? extends Object> downloadPDF(HttpServletRequest request,
        HttpServletResponse response, @RequestParam String docUri) {
    CheckOutDocumentResult out = null;
    try {
        out = eDashService.getDocument(docUri);
        String ext = out.getDocumentFileExtension();
        String fname = out.getDocumentFileName();
        if (ext != null && ext.toLowerCase().equals("pdf")) {
            response.setContentType("APPLICATION/pdf");
            String headerSetting = "attachment; filename=\"" + fname + "\";";
            response.setHeader("Content-Disposition", "attachment; filename=\""
                    + fname + "\";");
            InputStream is = new ByteArrayInputStream(out.getFileData());
            BufferedInputStream bis = new BufferedInputStream(is);
            OutputStream responseOutputStream = response.getOutputStream();
            int bytes;
            while ((bytes = bis.read()) != -1) {
                responseOutputStream.write(bytes);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (out.getDocumentFileName().equals("null")) {
        return geteDashMapError(out.getErrorMessage());
    }else{
        return geteDashMapStatus("no errors");
    }
}

我在这里做错了什么???

1 个答案:

答案 0 :(得分:0)

不要执行下载文件的Ajax请求。

点击你的链接做一个标准的提交请求文件,我在尝试下载Excel文件时也遇到了同样的问题。

修正了标准提交

的问题

这是一个讨论通过Ajax请求下载文件的线程。

Why threre is no way to download file using ajax request?

最好的问候。

相关问题