post提交下载文件请求

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

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<String, String> map, HttpServletRequest request,
            HttpServletResponse response) {
        try {
            this.downloadService.downloadSearch(map, request, response);
        } catch (CustomerSystemException e) {
            return ExceptionUtils.getStackTrace(e);
        }
        return "success";
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值