2

I would like to add a custom button in a backend grid page, that let me upload a file in Magento.

So basically it should give me a "file selector" and then file should be processed in a controller.

Can you point out to some tutorial or any Magento default module where I can get an "inspiration" ?

asked Jul 1, 2014 at 17:07

3 Answers 3

8

Try this.. In you block-form

$fieldset->addField('file', 'file', array(
 'label' => Mage::helper('foundation')->__('CSV'),
 'class' => 'disable',
 'required' => true,
 'name' => 'file',
 ));

And in your controller

public function saveAction()
 {
 if ($data = $this->getRequest()->getPost()) {
 if (isset($_FILES['file']['name']) && $_FILES['file']['name'] != '') {
 try
 {
 $path = Mage::getBaseDir().DS.'csv'.DS; //desitnation directory
 $fname = $_FILES['file']['name']; //file name
 $fullname = $path.$fname;
 $uploader = new Varien_File_Uploader('file'); //load class
 $uploader->setAllowedExtensions(array('CSV','csv')); //Allowed extension for file
 $uploader->setAllowCreateFolders(true); //for creating the directory if not exists
 $uploader->setAllowRenameFiles(false);
 $uploader->setFilesDispersion(false);
 $uploader->save($path, $fname); //save the
 }
 catch (Exception $e)
 {
 $fileType = "Invalid file format";
 }
 }
 }
 if ($fileType == "Invalid file format") {
 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('foundation')->__($fname." Invalid file format"));
 $this->_redirect('*/*/');
 return;
 }

........... I have done my changes as per the my requirement, you can try yours,

Hope this will help you

answered Jul 1, 2014 at 18:32
2

I used this tutorial https://stackoverflow.com/questions/6734281/magento-image-upload-form-field

Not going to re-write the same steps provided in the link since I'm referencing a different section of the same site.

answered Nov 6, 2015 at 15:05
1

Checkout the class Mage_Core_Model_File_Uploader

Maybe this source of the wysiwyg editor helps you

app/code/core/Mage/Cms/Model/Wysiwyg/Images/Storage.php:277
Manoj Deswal
5,80325 gold badges29 silver badges50 bronze badges
answered Jul 1, 2014 at 18:26
2
  • You should give more details about what you mean. This is a poor answer. Commented Jul 1, 2014 at 18:58
  • 1
    The question was to "point out to some tutorial or any Magento default module where he can get an "inspiration" " - I think my answer matches exactly the expectation. Commented Jul 3, 2014 at 8:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.