zoukankan      html  css  js  c++  java
  • 跨域form下载方式 批量下载

    downloadFileForm:function(fid) {
    
    var url = "https://file.xxxx.com/fileDownload.do";
    var inputs = "<input type='hidden' name='param' value='' />";
    var subNode = $('<form action="' + url + '" method="post" enctype="application/octet-stream">' + inputs + '</form>').appendTo('body');
    subNode.find("input").val("{'type'= 0, 'fileId'="+fid+",'time'=" + new Date().getTime() + "}");
    subNode.submit();
    },

    项目中需要跨域下载文件,要求浏览器不能弹出新窗口,找到了这个方法。

    重点是application/octet-stream

    关于批量下载的方法,但是却不能解决跨域的问题

     function down(downfile) {
    
    
                var fileExt = (/[.]/.exec(downfile)) ? /[^.]+$/.exec(downfile.toLowerCase()) : '';
                fileExt = fileExt[0];
    
                var xhr = new XMLHttpRequest();
    
    
                xhr.open('GET', downfile, true);
    
                xhr.responseType = 'blob';
                xhr.onload = function (e) {
                    if (this.status == 200) {
    
                        let res = this.response;  //不是responseText。
    
                        let blob = new Blob([res], {type: mimeType[fileExt]});
    
    
                        //  var blob = this.response;
                        console.log(blob);
    
                        var filename = "爱搜打打";
                        var a = document.createElement('a');
                        var url = URL.createObjectURL(blob);
                        a.href = url;
                        a.download = filename;
                        a.click();
                        window.URL.revokeObjectURL(url);
    
                    }
                };
                xhr.send();
    
            }
  • 相关阅读:
    Leetcode Binary Tree Level Order Traversal
    Leetcode Symmetric Tree
    Leetcode Same Tree
    Leetcode Unique Paths
    Leetcode Populating Next Right Pointers in Each Node
    Leetcode Maximum Depth of Binary Tree
    Leetcode Minimum Path Sum
    Leetcode Merge Two Sorted Lists
    Leetcode Climbing Stairs
    Leetcode Triangle
  • 原文地址:https://www.cnblogs.com/cfas/p/9144418.html
Copyright © 2011-2022 走看看