emrdev / symfony-dropzone
此包尚未发布版本,且信息有限。
README
扩展 SymfonyForm 组件。添加新的表单类型 DropzoneType
安装
composer require emrdev/symfony-dropzone
资源
将 dropzone 库添加到项目的模板中
<script src="https://unpkg.com/dropzone@6.0.0-beta.1/dist/dropzone-min.js"></script> <link href="https://unpkg.com/dropzone@6.0.0-beta.1/dist/dropzone.css" rel="stylesheet" type="text/css" />
用法
public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder, array $options) { // userFiles is OneToMany $builder->add('userFiles', DropzoneType::class, [ 'class' => File::class, 'maxFiles' => 6, 'uploadHandler'=>'uploadHandler', // route name 'removeHandler'=> 'removeHandler'// route name ]); }
示例:路由 uploadHandler/removeHandler
/** * @Route("/uploadhandler", name="uploadHandler") */ public function uploadhandler(Request $request, ImageUploader $uploader) { $doc = $uploader->upload($request->files->get('file')); $file = new File(); $file->setSrc($doc['src']); ... $this->getDoctrine()->getManager()->persist($file); $this->getDoctrine()->getManager()->flush(); return new JsonResponse($file); } /** * @Route("/removeHandler/{id}", name="removeHandler") */ public function removeHandler(Request $request,File $file = null) { $this->getDoctrine()->getManager()->remove($file); $this->getDoctrine()->getManager()->flush(); return new JsonResponse(true); }
选项
名称 | 默认值 | 描述 |
---|---|---|
class | null | 文件实体 |
choice_src | "src" | 包含 src 的属性 |
uploadHandler | null | 上传的 Symfony 路由名称 |
removeHandler | null | 删除的 Symfony 路由名称 |
multiple | true | 如果你有一个一对一的关系,则设置为 false |
maxFiles | 1 | 如果不为 null,则定义此 Dropzone 处理的文件数量。 |
addRemoveLinks | true | 如果为 true,则在每个文件预览中添加一个删除或取消(如果正在上传)文件的链接。 |
headers | [] | 一个可选对象,用于向服务器发送额外的标头。Headers 是数组。例如:['Authorization' => 'Bearer XXXXXX'] |
formData | [] | 将发送到 FormData 的额外数据。例如:['key' => 'value'] |
uploadHandlerMethod | "POST" | 如果需要,可以更改为 "PUT"。 |
removeHandlerMethod | "DELETE" | 如果需要,可以更改为 "POST"。 |
withCredentials | 0 | 将在 XHRequest 上设置。 |
thumbnailWidth | 120 | 如果为 null,则使用图像的比例来计算它。 |
thumbnailHeight | 120 | 与 thumbnailWidth 相同。如果两者都为 null,则不会调整图像大小。 |
thumbnailMethod | "crop" | 如果提供了 thumbnailWidth 和 thumbnailHeight,则图像应该如何缩小。可以是 contain 或 crop。 |
resizeWidth | null | 如果设置,则在上传之前将图像调整到这些尺寸。如果只提供了一个,则保留文件的原始宽高比。 |
resizeHeight | null | 参见 resizeWidth。 |
resizeMimeType | null | 调整图像上传到服务器之前的 MIME 类型。如果为 null,则使用原始 MIME 类型。例如,要强制使用 jpeg,请使用 image/jpeg。参见 resizeWidth 以获取更多信息。 |
resizeMethod | "contain" | 如果提供了 resizeWidth 和 resizeHeight,则图像应该如何缩小。可以是 contain 或 crop。 |
filesizeBase | 1024 | - |
ignoreHiddenFiles | true | 是否应忽略目录中的隐藏文件。 |
acceptedFiles | null | 例如:image/*,application/pdf,.psd |
autoProcessQueue | true | 如果为 false,则文件将添加到队列中,但队列不会自动处理。这可能在需要一些额外的用户输入才能发送文件(或希望一次发送所有文件)时很有用。如果你准备好了发送文件,只需调用 myDropzone.processQueue() 即可。 |
autoQueue | true | 如果为 false,则添加到 dropzone 的文件默认不会排队。您需要手动调用 enqueueFile(file)。 |
previewsContainer | null | 定义文件预览的显示位置 - 如果为空,则使用Dropzone元素本身。可以是CSS选择器。 |