简体   繁体   English

wordpress ajax导出到csv文件下载没有扩展名

[英]wordpress ajax export to csv file download without extension

In wordpress I want to generate csv file using ajax. 在wordpress中我想使用ajax生成csv文件。 So basically when user will click on button it will make an ajax call and in ajax php file it will make the csv and download it to the user system. 因此,基本上当用户点击按钮时它会进行ajax调用,而在ajax php文件中它会生成csv并将其下载到用户系统。 So for that I have done my code like this 所以为此我已经完成了我的代码

My markup is like this 我的标记是这样的

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

js code is like this 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;
    }
    }); 
});

My php code looks like this 我的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);
}

When I am doing click on the button it's doing download the file but without the extension .csv . 当我点击按钮时,它正在下载文件,但没有扩展名.csv。 I don't know what's happening here. 我不知道这里发生了什么。 Can someone tell me how to solve this issue? 有人能告诉我如何解决这个问题吗?

Looks like there is no way of specifying filename.extension using location.href or window.location.href . 看起来没有办法使用location.hrefwindow.location.href指定filename.extension。

It is possible to specify filename.extension if you instead emulate an anchor click: 如果您改为模拟锚点击,则可以指定filename.extension:

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;

See here: how to specify csv file name for downloading in window.location.href 请参见此处: 如何在window.location.href中指定要下载的csv文件名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM