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" ?
3 Answers 3
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
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.
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
- 
 You should give more details about what you mean. This is a poor answer.Marius– Marius2014年07月01日 18:58:52 +00:00Commented Jul 1, 2014 at 18:58
- 
 1The 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.Matthias Kleine– Matthias Kleine2014年07月03日 08:57:19 +00:00Commented Jul 3, 2014 at 8:57