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(); }