cancel
Showing results for 
Search instead for 
Did you mean: 

dropzone.js edit (add/delete) pre-exisitng image files in Magento

dropzone.js edit (add/delete) pre-exisitng image files in Magento

Hello

 

Below is my code for deleting images when clicked on "remove file" CTA on the dropzonejs box. However, when the "remove file" is clicked, only the thumbnail image is deleted, and not the actual file.

By calling the delete function in Magento through Ajax when the "removedfile" is called, it should delete the thumbnail as well as the file, but it does not seem to work as the server still keeps the pre-loaded image file.

 

this.on("removedfile", function(file) {
    alert("here");
        jQuery.ajax({
         url: "<?php echo Mage::getUrl("*/*/deleteimage"); ?>",
         type: "POST",
         dataType: "json",
         data: {'pid': "<?php echo $loadpro->getId(); ?>",'file':file.name},
     success: function (data) {
            Success = true;//doesnt goes here
        },
        error: function (textStatus, errorThrown) {
            jQuery("#vals").text('');
          var det = checkfile('<?php echo $loadpro->getId(); ?>');

          if(det != null)
          {
              maxcount = 1;
          }
          else {
            maxcount = null;
          }
        }

     });

Below is the code for the Magento controller that is called in the function above that listens for the "removedfile."

 

public function deleteimageAction()
  {
    $id= $this->getRequest()->getParam('pid');
    $imagename= $this->getRequest()->getParam('file');
    $product = Mage::getModel('catalog/product')->load($id);

    $productMediaConfig = Mage::getModel('catalog/product_media_config');
    $mediaGallery = $product->getMediaGalleryImages();

    foreach($mediaGallery as $mediagg){
      $file = $mediagg->getUrl();
      $fileName = basename($file);
      if($fileName == $imagename){
         $filepath = $mediagg->getFile();
      }
    }

    try{
      $storeId=Mage::app()->getStore()->getStoreId();
      Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
      $newimage = '';
      $data= $this->getRequest()->getParams();
      //error_log(print_r($data, TRUE));
      $_product = Mage::getModel('catalog/product')->load($data['pid'])->getMediaGalleryImages();
      $main = explode('/',$filepath);
      foreach($_product as $_image) {
        $arr = explode('/',$_image['path']);
        if(array_pop($arr) != array_pop($main)){
          $newimage = $_image['file'];
          $id = $_image['value_id'];
          break;
        }
      }
      $mediaApi = Mage::getModel("catalog/product_attribute_media_api");
      $mediaApi->remove($data['pid'],$filepath);
      if($newimage){
        $objprod=Mage::getModel('catalog/product')->load($data['pid']);
        $objprod->setSmallImage($newimage);
        $objprod->setImage($newimage);
        $objprod->setThumbnail($newimage);
        $objprod->save();
      }
      Mage::app()->setCurrentStore($storeId);
    } catch (Exception $e) {
             $this->getResponse()->setHeader('Content-type', 'text/html');
      $this->getResponse()->setBody('');
         }
  }

I have tried most of the methods that are in google, as well as stackoverflow, but could not find an answer that works in this case.

If you know a solution to this problem, please let me know.