dtz8044 2016-09-25 14:01
浏览 66

dropzone.js在Magento中编辑(添加/删除)预先存在的图像文件

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思