使用readfile在控制器中执行操作,其中名称由POST方法通过ajax调用传递


Action in the controller with readfile, where the name is passed by POST method through ajax call

这简直要了我的命。。。最愚蠢的事,但我看不出有什么意义。首先,我在服务器中使用phalcon。总之,我想要一个锚点运行一个控制器操作,它基本上是一个读文件(典型的保存为…窗口)。我做到了,但我没有得到的是使用ajax POST方法运行它,在调用中传递文件名(而不是在url中)。似乎我调用了操作,但没有使用变量,或者至少没有最终显示窗口。。。让我们看看一些代码:

public function descargarimagenAction(){
//Problem in the call, POST arrived here!
$nombre = $this->request->getPost("nombre");
$extension = "jpg";
$this->view->setRenderLevel('Phalcon'Mvc'View::LEVEL_NO_RENDER);
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header('Content-type: image/' . $extension);
header('Content-Disposition: attachment; filename="'.$nombre.'"');
readfile("img/".$nombre);
die();
}

和html:

<a class="btn btn-primary" href="notificaciones/descargarimagen" onclick="$.post(href,{nombre: 'imagendeprueba.jpg'},function(data){alert('done')});return false">

警报被激活,POST请求也被激活,但readfile或不起作用或不响应视图。有什么想法吗?问题是,通过href="notifications/descargarimagen/imagendeprueba.jpg"我可以下载,一切都很完美。(在函数参数BTW中使用一个简单的get变量,而不是getPost)。

首先,我建议您将POST得到的内容转储到服务器端,

var_dump($this->request->getPost())
die();

您可以从任何浏览器在开发人员控制台中检查结果。我不确定是否收到JSON,所以我建议您发送数据字符串模式

<a class="btn btn-primary" href="notificaciones/descargarimagen" onclick="$.post(href,'nombre=imagendeprueba.jpg'},function(data){alert('done')});return false">