Search code examples
jqueryfile-uploadjquery-file-uploadform-data

jQuery file upload can not set second parameter in formdata


I'm using jQuery-file-upload to upload files and set formdata as below:

actionUrl = '<c:url value="/nnmis/view/yjzh/addYjlx.tg"/>';
$('#fileupload').fileupload({
        url:actionUrl,
        formData:{pubdate:$('#formdate').datetimebox('getValue'),
            station:$('#formtype').combobox('getValue')
        },
        submit: function (e, data) {
            var station = $('#formtype').combobox('getValue');
            if(station == null || station==''){
                $.messager.alert('提示','所属站不能为空!','error');
                return false;
            }
        }
    });

at server side, the second parameter(station) in formdata is null. But if i set it to a constant the server side can get it:

formData:{pubdate:$('#formdate').datetimebox('getValue'),
            station:'sss'
        },

I'm sure the combobox of $('#formtype') have value but the server get null. Is it a bug of jQuery-file-upload? enter image description here

enter image description here

The server side is quite normal of a private String varible with setter and getter:

private String station;
public String getStation() {
    return station;
}

public void setStation(String station) {
    this.station = station;
}
public void add() {
    try {
        String person = request.getSession()
                .getAttribute("session_personid").toString();
        String username = request.getSession().getAttribute("fr_username")
                .toString();
        String org = request.getSession().getAttribute("session_orgid")
                .toString();
        String serverDir = ServletActionContext.getServletContext()
                .getRealPath("/");
        if (!"\\".equals(serverDir.substring(serverDir.length() - 1))) {
            serverDir = serverDir + "\\";
        }
        if (station == null || station.isEmpty()) {
            throw new Exception("请选择所属站!");
        }
        JsonArray jArray = new JsonArray();
        if (files != null) {
            String origin = "";
            String format = "";
            String savename = "";
            String savePath = "";
            File ofile = null;
            for (int i = 0; i < files.size(); i++) {
                ofile = files.get(i);
                origin = filesFileName.get(i);
                format = origin.substring(origin.lastIndexOf("."));
                File waitSaveFile = new File(serverDir + "cloudFiles\\"
                        + username + "\\" + origin);
                if (waitSaveFile.exists()) {
                    int index = 0;
                    String temporigin = origin.substring(0,
                            origin.lastIndexOf("."));
                    do {
                        index = index + 1;
                        waitSaveFile = new File(serverDir + "cloudFiles\\"
                                + username + "\\" + temporigin + "("
                                + index + ")" + format);
                    } while (waitSaveFile.exists());
                    savename = temporigin + "(" + index + ")" + format;
                } else {
                    savename = origin;
                }
                waitSaveFile = null;
                savePath = serverDir + "cloudFiles\\" + username + "\\"
                        + savename;
                File saveFile = new File(savePath);
                FileUtils.copyFile(ofile, saveFile);
                yjlxdao.save(model);
                JsonObject jObject = new JsonObject();
                jObject.addProperty("name", filesFileName.get(i));
                jObject.addProperty("size", 1);
                jObject.addProperty("delete_type", "GET");
                jArray.add(jObject);
            }
        }
        outJsonPlainString(response, "{\"error\":false,\"files\":" + jArray.toString() + "}");
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
        logger.error(e.getMessage());
        outJsonPlainString(response, "{\"error\":true}");
    }
}

Solution

  • You can try to use the fileuploadsubmit event like

    actionUrl = '<c:url value="/nnmis/view/yjzh/addYjlx.tg"/>';
    $('#fileupload').fileupload({
        url: actionUrl
    });
    
    $('#fileupload').bind('fileuploadsubmit', function (e, data) {
        data.formData = {
            pubdate: $('#formdate').datetimebox('getValue'),
            station: $('#formtype').combobox('getValue')
        };
        if (!data.formData.station) {
            $.messager.alert('提示', '所属站不能为空!', 'error');
                return false;
        }
    });