ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ## 文件异步下载 ```js <a class="js-ajax-down" href="javascript:void(0);" data-href="/fileinfo?gid=D3A5E74C-573B-EDD3-A89B-FD074FC33AB1" title="下载" >下载</a> <script type="text/javascript"> $(function () { $(".js-ajax-down").click(function () { //显示等待效果 var url = $(this).data('href'); $.getJSON(url,function (res) { if(res.status===0){ alert("请求出错"+res.msg); return ; } var down_url =res.data.down_url; var a = document.createElement('a'); a.href = down_url; $("body").append(a); a.click(); $(a).remove(); }); return false; }); }) </script> ``` ## 异步带下载百分比 异步下载文件需要在嵌套一层异步 ``` <!--region 文件下载样式--> <div id="filedown" style="display: none;width: 300px;margin-bottom: 10px;"> <p>下载:</p> <div class="uploadify-progress"> <div class="uploadify-progress-bar" style="width: 10%;"></div> </div> <span class="up_percent">0%</span><span class="uploadedsize set-file-style">0MB</span><span class="set-file-style">/</span><span class="totalsize set-file-style">0MB</span><span title="cn_windows_server_2008_r2_hpc_x64_dvd_552764.iso" class="up_filename">cn_windows_server_2008_r2_hpc_x64_dvd_552764.iso</span> </div> <!--endregion--> //使用了 progressJs 插件 var fileDownDom = $("#filedown"); $.post(url,data,function(res){ var res.info=res.downurl $.ajax({ type:'GET', url: res.url, xhrFields:{ onprogress:function(event){ var percent =(event.loaded/filesize)*100; percent = percent.toFixed(2); // 大于 1MB var loaded=0,totalSize=0,unit=''; if (filesize>1000000){ loaded = event.loaded/1000000; totalSize = filesize/1000000; unit="MB"; // 小于 1MB }else{ loaded = event.loaded/1000; totalSize = filesize/1000; unit="KB"; } fileDownDom.find('.uploadify-progress-bar').css('width',percent+'%'); fileDownDom.find('.up_percent').text(percent); fileDownDom.find('.uploadedsize').text(loaded.toFixed(2)+unit); fileDownDom.find('.totalsize').text(totalSize.toFixed(2)+unit); fileDownDom.find('.up_filename').text(res.info.file_name); } }, complete: function() { fileDownDom.hide(); // 如果发现无法下载.添加 此行 location.href = res.url ; }, }); },"JSON"); ```