Symfony2: upload a file using a file upload plugin

家住魔仙堡 提交于 2019-12-18 18:30:35

问题


I want to upload a text file received via AJAX request in Symfony2 (using Uploadify 2.1.4). How can I process it in an action? I've found some info in the official docs, but I guess it is not what I'm looking for.

For instance, I processed such a situation in Zend Framework this way:

    $adapter = new Zend_File_Transfer_Adapter_Http();
    $adapter->setFilters(array(
        'Rename' => array(
            'target'    => sprintf('%s/%d_%s', Zend_Registry::get('config')->uploads->uploadPath, time(), $adapter->getFileName(null, false), 'UTF-8'),
            'overwrite' => true,
        ),
    ));
    try
    {
        $adapter->receive();
    }
    catch (Zend_File_Transfer_Exception $e)
    {
        throw new Zend_Controller_Action_Exception(sprintf('Bad file: %s', $e->getMessage()));
    }

回答1:


I found the following simple solution. Maybe it'll be of help to somebody. ;)

The frontend part:

  $('#upload-file').uploadify(
    {
        script:         '/upload-file',
        folder:         '/uploads',
        method:         'POST',
        uploader:       '/bundles/mybundle/flash/uploadify.swf',
        cancelImg:      '/bundles/mybundle/images/cancel.png',
        buttonImg:      '/bundles/mybundle/images/upload.png',
        width:          48,
        height:         48,
        auto:           false,
        queueID:        'fileQueue',
        wmode:          'transparent',
        fileDataName:   'uploaded_file',
        fileDesc:       'Text File (*.txt)',
        fileExt:        '*.txt',
        sizeLimit:      8000000,
        multi:          true,
        simUploadLimit: 3,
        onError:        function (event, id, fileObj, errorObj)
        {
            console.log(errorObj.type + ' Error: ' + errorObj.info);
        }
    });

The backend part:

public function uploadFileAction()
{
    $request = $this->getRequest();
    $destination = preg_replace('/app$/si', 'web' . $request->request->get('folder'), $this->get('kernel')->getRootDir());
    $uploadedFile = $request->files->get('uploaded_file');

    $uploadedFile->move($destination, $uploadedFile->getClientOriginalName());

    return new Response(1);
}

The issue is closed!



来源:https://stackoverflow.com/questions/9201120/symfony2-upload-a-file-using-a-file-upload-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!