简体   繁体   English

保存在Cakephp中获取的多个图像

[英]Save multiple images fetched in Cakephp

I am trying to save multiple images on the server. 我正在尝试在服务器上保存多个图像。 Lets start from the beginning: 让我们从头开始:

//I use this function for testing
function testSave(){
    $this->_renderChart(156);
}

//This function takes chart_id as a parameter to render a proper chart.
function _renderChart($chart_id = null){
    if(!$chart_id)
        return false;
    $chartFilterList = $this->getChartFilterListFromId($chart_id);
    $this->loadChartFromId($chart_id, $chartFilterList);
    $this->layout = 'analytics\chart_one.ctp';
}

The above function's view contains all the necessary scripts to render the chart. 上面函数的视图包含呈现图表所需的所有脚本。 This is the part that is translating the rendered chart into base64string and saves it: 这是将呈现的图表转换为base64string并将其保存的部分:

//../views/layouts/analytics/chart_one.ctp
<script type="text/javascript">
    $(document).ready(function(){
        saveChartAsImage('#chart1');
    });
</script>

And the above function's body: 而以上函数的主体:

function saveChartAsImage(div){
    var base64string = $(div).jqplotToImageStr();
    $.ajax({
        url: 'saveImage',
        type: "POST",
        dataType: "html",
        data:"data=" + base64string
    });
}

} }

This is not even close to be working. 这甚至还差得远。 Am I doing something wrong here? 我在这里做错什么了吗?

If these functions are controller actions you can use $this->response->body() in controller's afterFilter() callback to get the response content and save it to file. 如果这些功能是控制器操作,则可以在控制器的afterFilter()回调中使用$this->response->body()获取响应内容并将其保存到文件中。 If these are helper functions you can get the content in afterLayout() or afterRender() callbacks of the helper using $this->_View->Blocks->get('content'); 如果这些是辅助函数,则可以使用$this->_View->Blocks->get('content');在辅助函数的afterLayout()afterRender()回调中$this->_View->Blocks->get('content');

I advise creating a behavior, and add the action beforeValidate() ; 我建议创建一个行为,然后在actionValidate()之前添加操作;
After that make the conditions necessary to invoke the specific actions (listed in your question). 之后,为调用特定操作(在问题中列出)提供必要的条件。

Example: 例:

app/Model/Behavior/ChartBehavior.php
class ChartBehavior extends ModelBehavior {

// if necessary, create a setup action
public function setup(Model $Model, $settings = array()) {  }

public function upload(Model $Model) {
    // use the $Model->data to see all information from your form
    debug($Mode->data);

    // now call your functions below to upload
}

public function line_chart($chart_id = null){
    //magic...
}

public function bar_chart($chart_id = null){
    //magic...
}

public function pie_chart($chart_id = null){
    //magic...
}

I hope it helps you. 希望对您有帮助。

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

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