文件上传,下载

Posted mandy3651423

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件上传,下载相关的知识,希望对你有一定的参考价值。

一.面板上传
该上传方式支持一次上传多个文件:
使用说明:
1.引入上传面板
var attachmentPanel = Ext.create("BeidaSoft.XTGL.attachment.AttachmentPanel", {
    height : 200,
    baseParams : {
        tableName : "test", // 业务表表名
        recordID : "testRecordID", // 业务表主键
        extParam : \'AAAAA\' // 面板标识,用于一个功能使用多个上传控件,对上传附件做区分
    }
});
2.界面展示

3.数据保存

attachmentPanel.baseParams.recordID = "testRecordID";
attachmentPanel.baseParams.tableName = "test";
attachmentPanel.baseParams.extParam = "AAAAA";
attachmentPanel.Save();

 

二.按钮上传

该上传方式一次只能上传一个文件

1.在js文件中加入
var
attachmentButton = Ext.create( "BeidaSoft.XTGL.attachment.AttachmentButtonN", { isSingle : true, fileTypes : "*.gif;*.pdf;*.jpg;*.doc;*.xls;*.docx;*.xlsx;*.rar;*.zip;*.png;*.rm;*.rmvb;*.avi;*.wmv;*.mp3", scale : \'medium\', id : \'attachmentButton\', cellCls : \'labelbutton\', baseParams : { extParam : \'AAAAA\' }, listeners : { uploadsuccess : function(serverData) { if (serverData.success = \'true\') { Ext.Ajax.request({ url : \'/sfjcgl/lzbahjwzhgl/zhgl/getFileName\', success : function(r) { var data = Ext.decode(r.responseText); if (data.fileName && data.fileName.length && data.fileName.length > 0) { var lxgb = data.fileName[data.fileName.length - 1]; Ext.getCmp(\'FJ\').show(); Ext.getCmp(\'FJ\').setValue(lxgb); Ext.getCmp("DeleteFile") .setVisible(true); } }, failure : function(r) { Ext.Msg.alert("提示", "查询失败!"); } }); } else { Ext.Msg.alert("提示", "上传失败!"); } } } }); this.attachmentButton = attachmentButton;

 将它单独作为一个form

var form2 = new Ext.form.FormPanel({
            baseCls : "",
            width : "99%",
            layout : "form",
            id : "form2",
            items : [{
                baseCls : "",
                layout : \'hbox\',
                items : [{
                            xtype : \'label\',
                            width : "15%",
                            text : \'具体内容:\',
                            name : "具体内容",
                            fieldLabel : "具体内容",
                            labelStyle : "font-size:\'5px\'",
                            style : {
                                textAlign : \'right\'
                            }
                        }, {
                            xtype : \'label\',
                            html : "<span id=\'DownloadFile\'></span>"
                        }, {
                            layout : \'hbox\',
                            xtype : \'textfield\',
                            width : 200,
                            id : \'FJ\'
                        }, {
                            xtype : \'button\',
                            id : "DeleteFile",
                            text : "删除附件",
                            listeners : {
                                "click" : function() {
                                    Ext.Ajax.request({
                                        url : "/sfjcgl/lzbahjwzhgl/zhgl/deleteAttachment",
                                        params : {
                                            recordId : me.PXBID,
                                            tableName : \'SFJC08\'
                                        },
                                        success : function(r) {
                                            Ext.getCmp("DeleteFile")
                                                    .setVisible(false);
                                            Ext.getCmp(\'FJ\').setValue(\'\');
                                            Ext.Msg.alert("提示", "附件删除成功!");
                                        },
                                        failure : function(r) {
                                            Ext.Msg.alert("提示", "删除附件失败!");
                                        }
                                    });
                                }
                            }
                        }, attachmentButton]
            }]
        });
        Ext.getCmp("DeleteFile").setVisible(false); // 隐藏删除按钮

点击保存时

Ext.Ajax.request({
                            url : \'/sfjcgl/lzbahjwzhgl/zhgl/savedata?theme=none\',
                            params : params,
                            success : function(response) {
                                var data = Ext.decode(response.responseText);
                                if (data.success == \'true\') {
                                    Ext.Msg.alert("提示", "操作成功!");
                                } else {
                                    Ext.Msg.alert("提示", "操作失败!");
                                }
                                attachmentButton.baseParams.recordID = data.uuid;
                                attachmentButton.baseParams.tableName = "SFJC08";
                                attachmentButton.baseParams.extParam = "AAAAA";
                                attachmentButton.Save();
                                me.up("window").destroy();
                                me.onPanelSuccessClose();
                            },
                            failure : function(response) {
                                Ext.Msg.alert("提示", "操作失败!");
                            }
                        });

2.在java文件中下载删除附件,取附件名称

/**
     * 下载附件
     */
    @Action
    public void download(){
        HttpServletRequest request = ActionContext.getActionContext().getHttpServletRequest();
        HttpServletResponse response = ActionContext.getActionContext().getHttpServletResponse();
        String fid = request.getParameter("fid");
        String downSql = "select FILEPATH,FILENAME from S_ATTACHMENT_FILE where FID = \'" + fid +"\'";
        Map<String, Object> downObj = jdbcOperations.queryForMap(downSql);
        String path = downObj.get("FILEPATH").toString();
        String filename = downObj.get("FILENAME").toString();
        File file = new File(path);
        // 以流的形式下载文件
        try {
            InputStream fis = new BufferedInputStream(new FileInputStream(path));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("gbk"), "iso-8859-1"));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // 取附件名称
        @Action
        public String getFileName() {
            HttpServletRequest request = ActionContext.getActionContext()
                    .getHttpServletRequest();
            HttpSession session = request.getSession();
            Map<String, ArrayList<String>> fileNameMap = new HashMap<String, ArrayList<String>>();

            ArrayList<Map<String, Object>> fileArray = new ArrayList<Map<String, Object>>();
            Object sessionObj = session.getAttribute("uploadList");
            if (null != sessionObj) {
                fileArray = (ArrayList<Map<String, Object>>) sessionObj;
                ArrayList fileNameArrayList = new ArrayList<String>();
                for (Map<String, Object> map : fileArray) {
                    if (null != map.get("filename")) {
                        fileNameArrayList.add((String) map.get("filename"));
                    }
                }
                fileNameMap.put("fileName", fileNameArrayList);
            }

            return Json.encode(fileNameMap);
        }
        @Action
        public void deleteAttachment() {
            HttpServletRequest request = ActionContext.getActionContext()
                    .getHttpServletRequest();
            String fileID = "";
            String recordId = request.getParameter("recordId");
            String tableName = request.getParameter("tableName");
            String ftype = request.getParameter("ftype");

            String querySQL = null;
            List<Map<String, Object>> list = null;
            if (null != ftype && !"".equals(ftype)) {
                querySQL = "select A1.FID from S_ATTACHMENT_RELATION A1 LEFT JOIN S_ATTACHMENT_FILE A2 ON A1.FID = A2.FID WHERE A1.TABLENAME=? AND A1.RECORDID = ? AND A2.FTYPE = ?";
                list = jdbcOperations.queryForList(querySQL, tableName, recordId,
                        ftype);
            } else {
                querySQL = "select FID from S_ATTACHMENT_RELATION WHERE TABLENAME=? AND RECORDID = ?";
                list = jdbcOperations.queryForList(querySQL, tableName, recordId);
            }
            if (list.size() > 0) {
                fileID = list.get(0).get("FID").toString();
            }

            /**
             * 删除保存的上传文件
             * 
             * 功能待实现
             * 
             */
            if (fileID != null && !"".equals(fileID) && !"null".equals("fileID")) {
                /**
                 * 删除文件记录表数据
                 */
                String deleteSql = "DELETE FROM S_ATTACHMENT_FILE WHERE FID = ? ";
                jdbcOperations.update(deleteSql, fileID);

                /**
                 * 删除数据文件关联表
                 */
                deleteSql = "DELETE FROM S_ATTACHMENT_RELATION WHERE FID = ? ";
                jdbcOperations.update(deleteSql, fileID);
            }
        }

 

以上是关于文件上传,下载的主要内容,如果未能解决你的问题,请参考以下文章

用于从 cloudkit 检索单列的代码模式/片段

大文件上传下载分片断点续传教程

Alamofire 文件上传出现错误“JSON 文本未以数组或对象开头,并且允许未设置片段的选项”

VIM 代码片段插件 ultisnips 使用教程

将存储在内存中的文件上传到s3

完全下载文件时,将下载的文件从一个片段传递到另一个片段