我有一个按钮,点击它会调用ajax函数。
这是我的ajax功能
function csv(){ ajaxRequest = ajax();//ajax() is function that has all the XML HTTP Requests postdata = "data=" + document.getElementById("id").value; ajaxRequest.onreadystatechange = function(){ var ajaxDisplay = document.getElementById('ajaxDiv'); if(ajaxRequest.readyState == 4 && ajaxRequest.status==200){ ajaxDisplay.innerHTML = ajaxRequest.responseText; } } ajaxRequest.open("POST","csv.PHP",false); ajaxRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); ajaxRequest.send(postdata); }
我根据用户输入创建csv文件。创建后,我希望它提示下载或强制下载(最好是强制)。我在PHP文件的末尾使用以下脚本下载文件。如果我在一个单独的文件中运行这个脚本工作正常。
$fileName = 'file.csv'; $downloadFileName = 'newfile.csv'; if (file_exists($fileName)) { header('Content-Description: File Transfer'); header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename='.$downloadFileName); ob_clean(); flush(); readfile($fileName); exit; } echo "done";