Symfony2:使用文件上传插件上传文件

时间:2012-02-08 20:45:52

标签: symfony uploadify zend-framework-modules

我想在Symfony2中上传通过AJAX请求收到的文本文件(使用Uploadify 2.1.4)。如何在动作中处理它?我在official docs找到了一些信息,但我想这不是我想要的。

例如,我用Zend Framework处理了这种情况:

    $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 个答案:

答案 0 :(得分:6)

我找到了以下简单的解决方案。也许这对某人有帮助。 ;)

前端部分:

  $('#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);
        }
    });

后端部分:

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);
}

问题已经结束!