Symfony2: upload a file using a file upload plugin

前端 未结 1 2044
感动是毒
感动是毒 2021-01-03 17:23

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, bu

相关标签:
1条回答
  • 2021-01-03 18:04

    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!

    0 讨论(0)
提交回复
热议问题