post提交下载文件请求

POST提交下载文件请求

  • html文件中有form表单
  • html文件没有form表单
  • 后台方法

在访问后台的下载文件接口时,一般使用打开新窗口的方式,代码如下:

window.open(url, "_blank");

但是此种方式使用的是get请求,不支持传递过多的参数,我们有时候需要使用post的方式向后台发送请求,此时,我们需要借助于html页面中的form表单来实现此功能。

html文件中有form表单

如果页面中已有form表单,方法如下:

function openDownloadSearchWindow(url) {
	var searchForm = document.getElementById("form_search");
    searchForm.method = "post";
    searchForm.action = url;
    searchForm.target = "_blank"; //打开新页面
    var searchCondition = new SearchCondition ();
    searchCondition.conditions = new Array ();
    $("#form_search input").each(function(index, element) {
        if ($(this).attr("displayType") == "check") {
            var hasCheck = $(this).is(':checked');
            if (hasCheck) {
                if (searchCondition.hasConditionBean(element.name)) {
                    var condition = searchCondition.getConditionBean(element.name);
                    var tmpElementValue = condition.value;
                    tmpElementValue += "|" + element.value;
                    searchCondition.replaceConditionBean (element.name, tmpElementValue, "Like");
                } else {
                    searchCondition.addConditionBean (element.name, element.value, "Like");
                }
            }
        }
    });
    var checkHiddenInput = [];
    var firstElement = searchForm.firstChild;
    for(j = 0,len=searchCondition.conditions.length; j < len; j++) {
        var conditionBean = searchCondition.conditions[j];
        var hideInput = document.createElement("input");
        hideInput.type = "hidden";
        hideInput.name = conditionBean.field; //后台要接受这个参数来取值
        hideInput.value = conditionBean.value; //后台实际取到的值
        searchForm.insertBefore(hideInput, firstElement);
        checkHiddenInput.push(hideInput);
    }
    if (document.all) {
        searchForm.attachEvent("onsubmit", function () { });        //IE
    } else {
        var subObj = searchForm.addEventListener("submit", function () { }, false);    //firefox
    }
    if (document.all) {
        searchForm.fireEvent("onsubmit");
    } else {
        searchForm.dispatchEvent(new Event("submit"));
    }
    searchForm.submit();
    for(j = 0,len=checkHiddenInput.length; j < len; j++) {
        searchForm.removeChild(checkHiddenInput[j]);
    }
}

html文件没有form表单

此时需要使用js创建一个表单并添加至页面,代码如下:

function openPostWindow(url, tableCode) {
    var tempForm = document.createElement("form");
    tempForm.id = "tempForm1";
    tempForm.method = "post";
    tempForm.action = url;
    tempForm.target = "_blank"; //打开新页面
    var hideInput1 = document.createElement("input");
    hideInput1.type = "hidden";
    hideInput1.name = "tableCode"; //后台要接受这个参数来取值
    hideInput1.value = tableCode; //后台实际取到的值
    /*var hideInput2 = document.createElement("input");
    hideInput2.type = "hidden";
    hideInput2.name="xtmc";
    hideInput2.value = data2;*/  //这里就是如果需要第二个参数的时候可以自己再设置
    tempForm.appendChild(hideInput1);
    //tempForm.appendChild(hideInput2);
    if (document.all) {
        tempForm.attachEvent("onsubmit", function () { });        //IE
    } else {
        var subObj = tempForm.addEventListener("submit", function () { }, false);    //firefox
    }
    document.body.appendChild(tempForm);
    if (document.all) {
        tempForm.fireEvent("onsubmit");
    } else {
        tempForm.dispatchEvent(new Event("submit"));
    }
    tempForm.submit();
    document.body.removeChild(tempForm);
}

后台方法

后台Controller类中的方法如下:

    @RequestMapping(value = "download/search", method = RequestMethod.POST, produces = "application/octet-stream")
    private String downloadSearch(@RequestParam(required = false) Map map, HttpServletRequest request,
            HttpServletResponse response) {
        try {
            this.downloadService.downloadSearch(map, request, response);
        } catch (CustomerSystemException e) {
            return ExceptionUtils.getStackTrace(e);
        }
        return "success";
    }

你可能感兴趣的:(Springboot)