當(dāng)我點擊導(dǎo)出按鈕時,我想下載從 php 文件中獲得的數(shù)據(jù)。唯一的問題是:當(dāng)我點擊按鈕時,會生成文件 csv 文件,但它說:file could not be opened, Failed-forbiddenHTML 代碼:<form id="form" method="POST"> <button id="export" name="export_btn" onclick="export_data();"><i class="fa fa-download" aria-hidden="true"></i> Export To CSV-File</button> <!--Export Btn: genereerd een csv file--></form>JavaScript 代碼:function export_data(){ event.preventDefault(); //zorgt ervoor dat je pagina niet onnodig herlaad als je op de btn drukt //deze function zorgt ervoor dat er een array wordt gemaakt van ID's van alle aangevingte checkboxes. var checkedIds = $(".chk:checked").map(function() { return this.id; }).toArray(); var Arr = JSON.stringify(checkedIds); //giet de array om in een JSON formaat zodat we ermee kunnen werken in PHP //aan de hand van ajax stuur ik de array door naar "delete_measurement.php" $.ajax({ type: "POST", url: "./export_measurement.php", data: {arr: Arr}, success: function(data){ //neem de data dat werd gegenereerd in de php file en download het in een csv file. var encodedUri = encodeURI(data); var link = document.createElement("a"); link.setAttribute("href", encodedUri); link.setAttribute("download", "airflow.csv"); document.body.appendChild(link); // Required for FF link.click(); //dit werkte nog niet helemaal. Via volgende link had ik mijn vraag gesteld: https://stackoverflow.com/questions/63846465/download-csv-file-by-using-php/63846748?noredirect=1#comment112902528_63846748 } }); }
1 回答

繁星點點滴滴
TA貢獻(xiàn)1803條經(jīng)驗 獲得超3個贊
由于您正在執(zhí)行 POST,因此您只需要從 PHP 代碼返回文件的內(nèi)容,并在成功事件中執(zhí)行以下操作:
var encodedUri = encodeURI(data);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "airflow.csv");
document.body.appendChild(link); // Required for FF
link.click();
- 1 回答
- 0 關(guān)注
- 198 瀏覽
添加回答
舉報
0/150
提交
取消