2

I have been using Magento native flash flex up-loader to upload image in my custom module.But when I upload image it gives me error like this Screencast Here

In the console, it redirects to 302 when upload action call.

Here is chrome console log Screencast

I have also checked permission for the uploader folder it's 777. also checked .htaccess file for redirection no issue in it.

Here is my code file

phtml file code.

 <?php $htmlId = "someHtmlId"; ?>
 <div id="<?php echo $htmlId ?>" class="uploader">
     <div class="buttons">
        <div id="<?php echo $htmlId ?>-install-flash" style="display:none">
            <?php echo Mage::helper('media')->__('This content requires last version of Adobe Flash Player. <a href="%s">Get Flash</a>', 'http://www.adobe.com/go/getflash/') ?>
        </div>
     </div>
     <div class="clear"></div>
     <div id="<?php echo $htmlId ?>-template" class="no-display">
         <div id="{{id}}" class="file-row">
             <span class="file-info">{{name}} ({{size}})</span>
             <span class="delete-button"><button id="{{id}}-delete" title="<?php echo $this->__("Delete")?>" type="button" class="scalable delete" onclick="uploader.removeFile('{{fileId}}')" style=""><span><span><span><?php echo $this->__("Delete") ?></span></span></span></button></span>
             <span class="progress-text"></span>
             <div class="clear"></div>
         </div>
     </div>
     <div id="<?php echo $htmlId ?>-template-progress" class="no-display">
         {{percent}}% {{uploaded}} / {{total}}
     </div>
 </div>

<script>
 var uploader = new Flex.Uploader('<?php echo $htmlId ?>', '<?php echo $this->getSkinUrl('media/uploader.swf') ?>', <?php echo $this->getConfigJson('some_parameter_for_you') ?>);
    uploader.onFilesComplete = function(completedFiles)
    {
        completedFiles.each(function(file)
            {
                doSomethingWithTheResponse(file.response);
                uploader.removeFile(file.id);
            }
        );
        MediabrowserInstance.handleUploadComplete();
    }
    // hide flash buttons
    if ($('<?php echo $htmlId ?>-flash') != undefined)
    {
        $('<?php echo $htmlId ?>-flash').setStyle({float:'left'});
    }
//specify maximum file upload size (very important!)
    var maxUploadFileSizeInBytes = 12345644;
    var maxUploadFileSize = '10M';
</script>

Block file code.

public function getConfigJson($field)
{
    $this->getConfig()->setParams(
        array(
            'form_key' => $this->getFormKey(),
            "field" => $field
        )
    );
    $this->getConfig()->setFileField('Filedata');
    $this->getConfig()->setUrl(Mage::getModel('adminhtml/url')->addSessionParam()->getUrl("*/*/upload", array("param1" => "value1")));
    $this->getConfig()->setFilters(array(
        'pdf documents' => array(
            'label' => Mage::helper('adminhtml')->__('Portable Document Format (.pdf)'),
            'files' => array('*.pdf')
        )
    ));
    return Mage::helper('core')->jsonEncode($this->getConfig()->getData());
}     

public function getConfig()
{
    if(is_null($this->_config)) {
        $this->_config = new Varien_Object();
    }

    return $this->_config;
}

Controller upload action

 public function uploadAction()
{
    if (!empty($_FILES))
    {
        $result = array();
        try
        {
            $uploader = new Varien_File_Uploader("Filedata");
            $uploader->setAllowRenameFiles(true);

            $uploader->setFilesDispersion(false);
            $uploader->setAllowCreateFolders(true);

            $path = "put_your_path_to_save_here";//ex. Mage::getBaseDir('base') . DS ."my_uploads" . DS

            $uploader->setAllowedExtensions(array('pdf')); //server-side validation of extension
            $uploadSaveResult = $uploader->save($path, $_FILES['Filedata']['name']);

            $result = $uploadSaveResult['file'];
        }
        catch(Exception $e)
        {
            $result = array(
                "error" => $e->getMessage(),
                "errorCode" => $e->getCode(),
                "status" => "error"
            );
        }             
        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
    }
}

Xml added js for flex uploader

 <reference name="head">
        <action method="addJs">
            <js>lib/flex.js</js>
        </action>
        <action method="addJs">
            <js>lib/FABridge.js</js>
        </action>
        <action method="addJs">
            <js>mage/adminhtml/flexuploader.js</js>
        </action>
    </reference>
1

I was digging in it and suddenly.......... got the answer

There is nothing major to do so..But it has taken too much effort to resolved.

May someone get rid to waste time that i did.Just to change below line in block file(Missing Form Key) and its working.............. wowwwwwwwwwwwwwww.

    public function getConfigJson($field)
{
    $this->getConfig()->setParams(
        array(
            'form_key' => Mage::getSingleton('core/session')->getFormKey(), /* Here form key was not getting */
            "field" => $field
        )
    );
    $this->getConfig()->setFileField('Filedata');
    $this->getConfig()->setUrl(Mage::getModel('adminhtml/url')->addSessionParam()->getUrl("*/*/upload", array("param1" => "value1")));
    $this->getConfig()->setFilters(array(
        'pdf documents' => array(
            'label' => Mage::helper('adminhtml')->__('Portable Document Format (.pdf)'),
            'files' => array('*.pdf')
        )
    ));
    return Mage::helper('core')->jsonEncode($this->getConfig()->getData());
}     

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.