Symfony2:使用文件上传插件上传文件 [英] Symfony2: upload a file using a file upload plugin

查看:173
本文介绍了Symfony2:使用文件上传插件上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

例如,我通过以下方式在Zend Framework中处理了这种情况:

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

推荐答案

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

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

前端部分:

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

问题已解决!

这篇关于Symfony2:使用文件上传插件上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆