我的多文件上传存在问题。我使用了一个包LcnFileUploaderBundle。
当我点击save
我的文件时,请不要保存在正确的文件夹中。我使用函数syncFilesFromTemp
。当我将功能更改为syncFilesToTemp
时,一切正常,但将我的文件保存在临时文件夹中。我的控制器和twig
文件与链接中的文件相同。
答案 0 :(得分:0)
我的控制员:
/**
* Edit Uploads for the given entity id
*
* In a real world scenario you might want to check edit permissions
*
* @param Request $request
* @param $entityId
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function editAction(Request $request, $entityId)
{
$editId = intval($entityId);
if ($editId < 1000000000) {
throw new \Exception('invalid editId');
}
$fileUploader = $this->container->get('lcn.file_uploader');
// $fileUploader1 = $this->container->get('lcn.file_manager');
$uploadFolderName = $this->getUploadFolderName($editId);
$form = $this->createFormBuilder()
->setAction($this->generateUrl('lcn_file_uploader_demo_edit', array('entityId' => $entityId)))
->setMethod('POST')
->getForm();
if ($request->getMethod() == 'POST') {
$form->submit($request);
if ($form->isValid()) {
$fileUploader->syncFilesFromTemp($uploadFolderName);
return $this->redirect($this->generateUrl('addItemGallery', array('entityId' => $entityId)));
}
} else {
$fileUploader->syncFilesToTemp($uploadFolderName);
}
return $this->render('PortfolioAdminBundle:Gallery:edit.html.twig', array(
'entityId' => $entityId,
'form' => $form->createView(),
'uploadUrl' => $this->generateUrl('lcn_file_uploader_demo_handle_file_upload', array('entityId' => $entityId)),
'uploadFolderName' => $uploadFolderName,
));
}
_
/**
* Store the uploaded file.
*
* In a real world scenario you might probably want to check
* if the user is allowed to store uploads for the given entity id.
*
* Delegates to LcnFileUploader which implements a REST Interface and handles file uploads as well as file deletions.
*
* This action must not return a response. The response is generated in native PHP by LcnFileUploader.
*
* @param Request $request
* @param int $userId
*/
public function handleFileUploadAction($entityId, Request $request)
{
$entityId = intval($entityId);
if ($entityId < 100000000) {
throw new AccessDeniedHttpException('Invalid edit id: '.$entityId);
}
$this->container->get('lcn.file_uploader')->handleFileUpload(array(
'folder' => $this->getUploadFolderName($entityId),
//'max_number_of_files' => 3, //overwrites parameter lcn_file_uploader.max_number_of_files
//'allowed_extensions' => array('jpg'), //overwrites parameter lcn_file_uploader.allowed_extensions
//'sizes' => array('thumbnail' => array('folder' => 'thumbnail', 'max_width' => 100, 'max_height' => 100, 'crop' => true), 'profile' => array('folder' => 'profile', 'max_width' => 400, 'max_height' => 400, 'crop' => true)), //overwrites parameter lcn_file_uploader.sizes
));
}
#
public function handleFileUpload($options = array())
{
if (!isset($options['folder']))
{
throw new \Exception("You must pass the 'folder' option to distinguish this set of files from others");
}
$options = array_merge($this->options, $options);
$allowedExtensions = $options['allowed_extensions'];
// Build a regular expression like /(\.gif|\.jpg|\.jpeg|\.png)$/i
$allowedExtensionsRegex = '/(' . implode('|', array_map(function($extension) { return '\.' . $extension; }, $allowedExtensions)) . ')$/i';
$sizes = (isset($options['sizes']) && is_array($options['sizes'])) ? $options['sizes'] : array();
$tempFilePath = $options['temp_file_base_path'] . '/' . $options['folder'];
$tempWebPath = $options['temp_web_base_path'] . '/' . $options['folder'];
foreach ($sizes as $index => $size)
{
$sizes[$index]['upload_dir'] = $tempFilePath . '/' . $size['folder'] . '/';;
$sizes[$index]['upload_url'] = $tempWebPath . '/' . $size['folder'] . '/';
$sizes[$index]['no_cache'] = true;
}
$uploadDir = $tempFilePath . '/' . $this->getOriginalFolderName() . '/';;
$uploadUrl = $tempWebPath . '/' . $this->getOriginalFolderName() . '/';
foreach ($sizes as $size)
{
@mkdir($size['upload_dir'], 0777, true);
}
@mkdir($uploadDir, 0777, true);
new $this->options['upload_handler_class'](
array(
'file_namers' => $options['file_namers'],
'upload_dir' => $uploadDir,
'upload_url' => $uploadUrl,
'image_versions' => $sizes,
'accept_file_types' => $allowedExtensionsRegex,
'max_number_of_files' => $options['max_number_of_files'],
'max_file_size' => $options['max_file_size'],
));
// Without this Symfony will try to respond; the BlueImp upload handler class already did,
// so it's time to hush up
exit(0);
}
和树枝:
{% block body %}
Demo usage of LcnFileUploaderBundle
Edit Uploads of temporary entity {{ entityId }}
{{ form_start(form, { 'attr': { 'id': 'lcn-file-uploader-demo' } }) }}
{{ form_errors(form) }}
{% include 'PortfolioAdminBundle:Theme:lcnFileUploaderWidget.html.twig' with {
'uploadUrl': uploadUrl,
'uploadFolderName': uploadFolderName,
'formSelector': '#lcn-file-uploader-demo'
} %}
{{ form_rest(form) }}
<input type="submit" value="save">
{% endblock %}
{{ form_errors(form) }}
{% include 'PortfolioAdminBundle:Theme:lcnFileUploaderWidget.html.twig' with {
'uploadUrl': uploadUrl,
'uploadFolderName': uploadFolderName,
'formSelector': '#lcn-file-uploader-demo'
} %}
{{ form_rest(form) }}
<input type="submit" value="save">
的routing.yml