问题
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