window.open() 以post方式下载文件

1、正常下载文件

 window.open("url", "_blank");

2.post方式

 function openPostWindow(url, data) {
            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 = "accounts"; //后台要接受这个参数来取值
            hideInput1.value = data; //后台实际取到的值
            /*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);
        }

猜你喜欢

转载自www.cnblogs.com/fightingkaikai/p/11307055.html