简体   繁体   English

如何使用CakePHP和Ajax删除文件?

[英]How can I delete files with CakePHP and Ajax?

I'm in a page called 'add.cpt' that has a list of images. 我在一个名为“ add.cpt”的页面中,其中包含图像列表。 The user has the option to remove the images but I can't make it work. 用户可以选择删除图像,但我无法使其正常工作。 In the event of click I try to call an ajax trying to pass the name of the image and id of the item (.../item/imageName) but it does delete the image and alerts what seems to be the content of delete_photo_file.ctp. 在单击的情况下,我尝试调用一个ajax,尝试传递图像的名称和项目的ID(... / item / imageName),但它确实删除了图像并警告delete_photo_file的内容。 ctp。 It looks like the ajax is using the URL but it is not sending the data to delete the file wanted. 看起来ajax正在使用URL,但未发送数据来删除想要的文件。

ItemsController: ItemsController:

App::uses('File', 'Utility');
class ItemsController extends AppController{

[...]

public function deletePhotoFile(){
  //$this->autoRender = false; //did not tested but maybe I need to use this
  $imgName = //don't know how to get it from ajax call
  $itemId = //don't know how to get it from ajax call
  $file = new File($dir.'/'.$itemId.'/'.$imgName);
  $file->delete();
} 

}

Ajax Call (from my ctp file): Ajax通话(来自我的ctp文件):

$('#delete').click(function (){
[...]

var itemId=$('#itemId').val(); //comes from hidden input
var imgName = $('#imgName').val(); //comes from hidden input

$.ajax({
  type: 'POST',
  url:'http://localhost/html/Project/v5/CakeStrap/items/deletePhotoFile/',
  data:{"itemId":itemId, imgName: imgName},
  success: function(data){
    alert(data); //alerts some HTML... seems to be delete_photo_file.ctp content
  }
});

});

Can anyone help me? 谁能帮我? Thanks! 谢谢!

In your ItemsController, make sure you actually load the File utility class, by adding: 在您的ItemsController中,通过添加以下内容,确保您实际上加载了File实用程序类:

App::uses('File', 'Utility');

Just below the opening <?php tag before your class definition. 在类定义之前的<?php标记下方。 In your action you can just use $this->request->data to get the data keys. 在您的操作中,您可以仅使用$this->request->data获取数据密钥。 Also, return the action of the delete() function, so you can trigger your AJAX success/error call accordingly. 另外,返回delete()函数的操作,以便可以相应地触发AJAX成功/错误调用。

public function deletePhotoFile() {
    $imgName = $this->request->data['imgName'];
    $itemId = $this->request->data['itemId'];
    /**
     * Where is the $dir below actually set? Make sure to pass it properly!
     * Furthermore, it's always cleaner to use DS constant
     * (short for DIRECTORY_SEPARATOR), so the code will work on any OS
     */
    $file = new File($dir . DS . $itemId . DS . $imgName);
    return $file->delete();
} 

Finally, mind your quotes in the AJAX call: 最后,请注意AJAX调用中的引号:

data:{"itemId":itemId, imgName: imgName},

Should become: 应该变成:

data:{"itemId":itemId, "imgName": imgName},

As otherwise, you just call the imgName JS var twice. 否则,您只需两次调用imgName JS var。

In the php $imgName = $this->request->data('imgName'); $itemId = $this->request->data('imgId'); 在PHP中$imgName = $this->request->data('imgName'); $itemId = $this->request->data('imgId'); $imgName = $this->request->data('imgName'); $itemId = $this->request->data('imgId'); In the js you might want to put quotes around the variable name since it's the same as the name of the value being passed data: {'itemId': itemId, 'imgName': imgName}, 在js中,您可能需要在变量名data: {'itemId': itemId, 'imgName': imgName},加上引号,因为它与要传递的data: {'itemId': itemId, 'imgName': imgName},的值的名称相同data: {'itemId': itemId, 'imgName': imgName},

To get the data out simply debug($this->request->data) in your deletePhotoFile() method and check the response in your browsers console, it should be a nicely formatted array with the data you POST`d over in your ajax request, you should be able to work out the rest from there. 要获取数据,只需在deletePhotoFile()方法中debug($this->request->data)并在浏览器控制台中检查响应,它应该是一个格式良好的数组,其中包含您在ajax中发布的数据要求,您应该可以从那里算出其余的。

You'll also want to look into using the RequestHandler component so you can assure the request is an ajax request. 您还需要研究使用RequestHandler组件,以便确保该请求是ajax请求。

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

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