js接收文件流并下载

js接收文件流并下载

标签(空格分隔): js

在此输入正文

<script type="text/javascript">
    function download(filename) {
        var oReq = new XMLHttpRequest();
        oReq.open("GET", "<你的返回流的Action路径>", true);
        oReq.responseType = "blob";
        oReq.onload = function (oEvent) {
            var content = oReq.response;

            var elink = document.createElement('a');
            elink.download = filename;
            elink.style.display = 'none';

            var blob = new Blob([content]);
            elink.href = URL.createObjectURL(blob);

            document.body.appendChild(elink);
            elink.click();

            document.body.removeChild(elink);
        };
        oReq.send();
    }
</script>
posted @ 2019-05-18 17:28  TaylorSWMM  阅读(8941)  评论(0编辑  收藏  举报