try 从SQL数据库查询以CSV格式下载结果时,浏览器找不到文件.

大家好.我在我的HTML脚本中有以下函数.我希望能够下载用户查询的结果作为CSV文件.当我呈现网页并查询数据库时,工具栏出现;但是,当我try 下载结果的CSV时,我的浏览器找不到文件.如果有人指出我的错误所在,我将不胜感激.谢谢!

function google_piechart() {
            
            var class_choice = $('#class_choice').val();
            if (class_choice == null) {
                alert('Please select a class.');
                return false;
                }
            console.log('Selected class:', class_choice);

            // get data from output of Python script
            $.get("https://bioed.bu.edu/cgi-bin/students_23/Team_H/class_cgi.py",
                {selector: 'piechart',  class_choice:class_choice},
                // need to use the Google charts pie chart function here to generate from data...
                function (res){
                    console.log('Received data:', res);
                    create_piechart(res);
                },
                "json"
            );
        }
        
        
        // define that function here, now
        function create_piechart(res, class_choice) {
            console.log('Processed data:', res, class_choice);
            global_data1 = res

            // use strict equality operator here...don't want weird results from just `==`

            if (res.length === 0)
                $('#piechart').html('There was no data for the class choice: ' + class_choice);
         

            let formatted_results = res.map(item => item.slice(0, 2));
            console.log(formatted_results);
            // adding a header
            formatted_results.unshift(["Transposon Order", "Count"]);

            // get the total count of results
            let total_count = res.reduce((sum, item) => sum + item[1], 0);
            console.log('Total count:', total_count)
    
            // return total count to the screen for the user
            $('#result_count').html('Total number of results: ' + total_count);


            // loading Google charts info
            google.charts.load('current', {'packages': ['corechart', 'table', 'gauge']});
            google.charts.setOnLoadCallback(function () {
                // format processed data for use w/ Google charts
                var chart_data = google.visualization.arrayToDataTable(formatted_results);
                console.log('Chart data:', chart_data);
                // options for the piechart
                var options = {

                    title: 'Transposon Orders by Class',
                    fill: 'transparent'
                    is3D: true,
                    colors: ['#0072B2', '#E69F00', '#009E73', '#F0E442', '#CC79A7', '#D55E00', '#56B4E9', '#0072B2', '#F0E442', '#009E73', '#D55E00', '#CC79A7']
                };

                // finally creating the pie chart
                var chart = new google.visualization.PieChart(document.getElementById('piechart'));
                chart.draw(chart_data, options);

                // create a table with the same data
                var table = new google.visualization.Table(document.getElementById('table1'));
                // execute here
                table.draw(chart_data, {
                    title: 'Transposon Orders by Class',
                    showRowNumber: false, 
                    width: '50%'
                });
        
                // convert data table to CSV format
                var csv_data = google.visualization.dataTableToCsv(chart_data);
                console.log('CSV data:', csv_data)

                // draw the toolbar
                var components = [
                {type: 'csv', datasource: csv_data}
                ];


                var toolbarContainer = document.getElementById('toolbar_div1');
                google.visualization.drawToolbar(toolbarContainer, components);
            });
        }

推荐答案

我从另外thread on Stack Overflow个中找到了自己的解决方案.您可以使用如下所示的简单函数下载传递到Google图表片段图中的数据:

$('#Export').click(function () {
    var csvFormattedDataTable = google.visualization.dataTableToCsv(chart_data);
    var encodedUri = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csvFormattedDataTable);
    this.href = encodedUri;
    this.download = 'orders_by_class.csv';
    this.target = '_';
});

其中,Export ID是锚.

Javascript相关问答推荐

使用reaction创建可排序的表不起作用

JavaScript Date对象在UTC中设置为午夜时显示不正确的日期

如何在不使用类型化数组的情况下将32位浮点数按位转换为整值?

如何在加载的元数据上使用juserc和await中获得同步负载?

D3多线图显示1线而不是3线

如何分配类型脚本中具有不同/额外参数的函数类型

TypScript界面中的Infer React子props

Cypress -使用commands.js将数据测试id串在一起失败,但在将它们串在一起时不使用命令有效

Angular中计算信号和getter的区别

MathJax可以导入本地HTML文档使用的JS文件吗?

我的服务工作器没有连接到我的Chrome扩展中的内容脚本.我该怎么解决这个问题?

编辑文本无响应.onClick(扩展脚本)

如何在DYGRAPS中更改鼠标事件和键盘输入

触发异步函数后不能显示数据

当标题被点击时,如何使内容出现在另一个div上?

如何防止ionic 输入中的特殊字符.?

如何访问此数组中的值?

为什么延迟在我的laravel项目中不起作用?

处理app.param()中的多个参数

如何更改Html元素S的 colored颜色 ,然后将其褪色为原始 colored颜色