1

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

例如,我在 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()));
    }
4

1 回答 1

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

问题已关闭!

于 2012-02-09T19:19:44.190 回答