データベースからCSVファイルのユーザーデータとしてダウンロードする小さなWordpress Pluginを開発しました。ダウンロード後に成功メッセージを表示する
成功メッセージを表示する方法を理解する上で問題はありますが、すべて正常に動作します。
データエクスポートの世話をする方法のコードの下:私はそれ以外の場合はCSVファイルには、ページのすべてのHTMLが含まれています出口を使用する必要が
/*
* Export CSV File
*/
public function export_CSV()
{
if ($this->users) {
$fileName = "Export_users_" . date("Y-m-d_H:i:s", time());
$fileName = $fileName.".csv";
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header('Content-Encoding: UTF-8');
header("Content-type: application/csv;charset=UTF-8");
header("Content-Disposition: attachment; filename={$fileName}");
header("Expires: 0");
header("Pragma: public");
echo "\xEF\xBB\xBF"; // UTF-8 BOM
$fh = @fopen('php://output', 'w');
$headerDisplayed = false;
foreach ($this->users as $user) {
if($user) {
$user_meta = get_user_meta($user->data->ID);
//Push user meta into $user array
foreach($user_meta as $key => $var)
{
$user->data->$key = trim($var[0]);
}
//convert object in array;
$usersArray = (array)$user->data;
// Add a header row if it hasn't been added yet
if ($headerDisplayed == false) {
// Use the keys from $directory as the titles
fputcsv($fh, array_keys($usersArray));
$headerDisplayed = true;
}
// Put the data into the stream
fputcsv($fh,$usersArray);
}//end if
}//end foreach
// Close the file
fclose($fh);
// Users exported successfully
$this->response = "Users exported successfully!";
// Make sure nothing else is sent
exit;
} else {
// No results found
$this->response = "Sorry, no results found!";
}
}
。フロントエンドでメッセージを更新するためのjavascriptファイルを実行する方法はありますか?
ありがとうございます!
別のページで、ダウンロードのものを入れてください。ヘッダーは一度だけ送信できます。そのため、csvヘッダーを送信した後にhtmlを印刷できません。 – modsfabio
私の知る限りでは、ダウンロードが完了した後に何かを実行する方法がありません。私はあなたがServerSentEventsまたは何かを使うことができると思うが、それは一番上にあるようだ。 – Halcyon