我使用属性file
创建了实体,如下所示:
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class Archivo {
/**
* @var File
*
* @Assert\File(
* maxSize = "6M",
* mimeTypes = {"application/pdf", "application/x-pdf",
* "application/x-rar-compressed","application/octet-stream", "application/zip"},
* maxSizeMessage = "Symfony: File too big!",
* mimeTypesMessage = "Symfony: Invalid file type!"
* )
*/
protected $file;
}
我使用此方法存储文件:
public function upload($file, $path) {
if (null === $file) {
return;
}
$name = $file->getClientOriginalName();
$file->move(
$path, $name
);
unset($file);
}
这是我的表格:
class ArchivoType extends AbstractType {
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('file', 'file')
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(array(
'data_class' => 'SisEvo\UploadBundle\Entity\Archivo'
));
}
}
当我使用RAR文件提交表单时,会显示警告Symfony: Invalid file type!
。如何上传RAR,CSV,DOC,DOCX,XLS和XLSX文件?
提前谢谢!
答案 0 :(得分:0)
我解决了自己的问题;正如@Alsatian所说,我的RAR不是'application / x-rar-compressed'。 我使用此website来检查我的RAR文件的MIME类型。我发现我的RAR文件是“application / x-rar”。
感谢您的时间!