WordPress Ajax导出到CSV文件下载,不带扩展名


wordpress ajax export to csv file download without extension

在wordpress中,我想使用ajax生成csv文件。所以基本上当用户点击按钮时,它会进行ajax调用,在ajax php文件中,它将制作csv并将其下载到用户系统。所以为此,我像这样完成了我的代码

我的标记是这样的

<button type="button" class="btn export-csv" data-proj-id="1">Export CSV</button>

js 代码是这样的

$('body').on('click', 'button.export-csv', function() {
    var proj_id = $(this).attr('data-proj-id');
    $.ajax({
        type:"POST",
        url:trans.ajaxUrl,
        data:'proj_id='+proj_id+'&action=export_client_price_csv',
        success: function (data) {
            var uri = 'data:application/csv;charset=UTF-8,' + encodeURIComponent(data);
      location.href = uri;
    }
    }); 
});

我的 php 代码看起来像这样

function export_client_price_csv() {
    global $wpdb;
    $proj_id = $_POST['proj_id'];
    $user_id = get_current_user_id();
    $site_id = get_current_blog_id();
    if( !empty($proj_id) ) {
        $get_prices_data = $wpdb->get_results("SELECT * FROM `fl_client_price` WHERE `user_id` = ".$user_id." AND
        `project_id` = ".$proj_id." AND `site_id` = ".$site_id." AND `client_status` LIKE '%invoiced%' ");
        $data_array = array();
        if( count($get_prices_data) > 0 ) {
            foreach( $get_prices_data as $data ) {
                $array = get_object_vars($data);
                array_push($data_array, $array);
                convert_to_csv($data_array, 'report.csv', ',');
            }
        }
    }
    exit;
}
add_action( 'wp_ajax_export_client_price_csv', 'export_client_price_csv' );
function convert_to_csv($input_array, $output_file_name, $delimiter)
{
    /** open raw memory as file, no need for temp files, be careful not to run out of memory thought */
    $f = fopen('php://memory', 'w');
    /** loop through array  */
    foreach ($input_array as $line) {
        /** default php csv handler **/
        fputcsv($f, $line, $delimiter);
    }
    /** rewrind the "file" with the csv lines **/
    fseek($f, 0);
    /** modify header to be downloadable csv file **/
    header('Content-Type: text/html; charset=UTF-8');
    //header('Content-Type: application/csv');
    header('Content-Disposition: attachement; filename="' . $output_file_name . '";');
    /** Send file to browser for download */
    fpassthru($f);
}

当我单击按钮时,它正在下载文件,但没有扩展名.csv。我不知道这里发生了什么。有人可以告诉我如何解决这个问题吗?

看起来没有办法使用 location.href 或 window.location.href 指定文件名.扩展名。

如果您

改为模拟锚点单击,则可以指定文件名.扩展名:

var csvdata = "Hello World"; //  only for test
var byteNumbers = new Uint8Array(csvdata.length);
for (var i = 0; i < csvdata.length; i++)
{
    byteNumbers[i] = csvdata.charCodeAt(i);
}
var blob = new Blob([byteNumbers], {type: "text/csv"});
// Construct the uri
var uri = URL.createObjectURL(blob);
// Construct the <a> element
var link = document.createElement("a");
link.download = 'myfile.csv';
link.href = uri;
document.body.appendChild(link);
link.click();
// Cleanup the DOM
document.body.removeChild(link);
delete link;

请参阅此处:如何在 window.location.href 中指定用于下载的 csv 文件名