亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用從 ajax 調用返回的 html 下載文件

使用從 ajax 調用返回的 html 下載文件

蝴蝶不菲 2023-03-18 11:09:45
我正在嘗試使用從 ajax 調用返回的 html 在同一頁面上下載文件。ajax 調用如下所示:                            $.ajax({                                url: './xyz.cshtml',                                type: "POST",                                                               data: {                                    //some validation and data to send to server.                                },                                success: function (data, status, xhr) {                                    //alert('status: ' + status + ', data: ' + data);                                    var result = $(data);                                                                        console.log(result);                                    result.appendTo("body");                                }                             });HTML 看起來像這樣:<iframe width='1' height='1' frameborder='0' src='http://server//some_folder/some.xlsm'></iframe> <script>document.body.style.cursor = 'default';</script>基本上,我想解析這個 iframe,然后在同一頁面上運行它。當 iframe 中的代碼運行時,文件將下載。因此,我需要幫助將此 html 解析為頁面,然后運行此代碼塊。提前致謝。
查看完整描述

1 回答

?
冉冉說

TA貢獻1877條經驗 獲得超1個贊

好吧,首先你想從 iframe 中獲取 src,然后創建錨點 dom 元素并單擊它。我想這應該可以解決問題


var ajaxResponse = "<iframe width='1' height='1' frameborder='0' src='http://server//some_folder/some.xlsm'></iframe> <script>document.body.style.cursor = 'default';</script>";


// this function will simply download the file

function download(filename, dataurl) {

  var a = document.createElement("a");

  a.href = dataurl;

  a.setAttribute("download", filename);

  a.click();

}


var regex = /<iframe.*?src='(.*?)'/;

var src = regex.exec(ajaxResponse)[1]; // src from that iframe


download("SomeFileName.xmlsm", src);


查看完整回答
反對 回復 2023-03-18
  • 1 回答
  • 0 關注
  • 254 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號