【问题标题】:How to download canvas image from Database INSIDE datatables如何从数据库 INSIDE 数据表中下载画布图像
【发布时间】:2019-12-28 02:36:52
【问题描述】:

我有一个保存在 MYSQL 数据库中的画布图像,可以在使用数据表和 PHP 的表中看到,但我无法下载图像。

这是我的 JS 文件,它将 ajax 请求发送到服务器:

$(document).ready(function(){
  var data = $('#dataList').DataTable({
    "lengthChange": false,
    "processing":true,
    "order":[],
    "ajax":{
        url:"/php/process.php",
        type:"POST",
        data:{action:'listData'},
        dataType:"json"
    },
    "columnDefs":[
        {
            "targets":[0, 5, 6],
            "orderable":true,
        },
    ],
    "pageLength": 10
  });

这里是process.php

$sqlQuery = "SELECT * FROM table1 AS a LEFT JOIN sketch AS s ON a.id= s.id";

$auftragData = array();
$result = $this->dbc -> prepare($sqlQuery);
$result -> execute();

 while ( $tableResult= $result->fetch(PDO::FETCH_ASSOC) ) {
  $resultRows = array();
  $resultRows[] = $tableResult['id'];
  $resultRows[] = ucfirst($tableResult['cust_id']);
  $resultRows[] = $tableResult['typ'];
  $resultRows[] = $tableResult['status'];
  $resultRows[] = $tableResult['sketch'];
  if ($tableResult['sketch']) {
      $resultRows[] = '<a id="download" download="sketch.png"><button type="button">Download Image</button></a>';
        }
   $resultRows[] = '<button type="button" name="update" id="'.$tableResult["id"].'" class="btn btn-warning btn-xs update">update</button>';
   $resultRows[] = '<button type="button" name="delete" id="'.$tableResult["id"].'" class="btn btn-danger btn-xs delete" >delete</button>';
 $finalData[] = $auftragRows;
        }
$numRows = $result -> rowCount();

    $output = array(
        "draw"          =>  $numRows,
        "recordsTotal"      =>  $numRows,
        "recordsFiltered"   =>  $numRows,
        "data"          =>  $finalData
        );
        echo json_encode($output);
      $this->dbc = NULL;
      exit;
    }

图片网址是: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA4QAAAH0CAYAAABl8+PTAAAgAElEQVR4Xu3dB9glZ103/u8LJCHhpQkYCUWlWwAFFZDQO0F6lUAkFEEpghQpvuArRboUAaU36YYWpBMhFP1Lk1clIhZAOoIgCUko/+uHs/pks7

上面的代码如何下载图片?

【问题讨论】:

  • 上述问题是关于从客户端进行的简单下载。我的 URL 保存在服务器端,我想将其检索到客户端,然后进行下载。这里的问题是数据表
  • 究竟是什么问题?
  • @VladimirSerykh 我已经实现了您发布的上述链接中提到的方式。我知道使用:
  • $resultRows[] = '';函数 downloadSkizze() { var download = document.getElementById('downloadSketch'); download.setAttribute("href", 下载); } 但我只得到一个黑屏图像。有些东西不工作。

标签: php jquery html mysql datatables


【解决方案1】:

发现问题。 MySQL 数据库中的字段是字符切割,同时将图像保存在服务器上。这样就解决了问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-06
    • 2015-11-09
    • 2015-01-10
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    相关资源
    最近更新 更多