您好我正在尝试使用以下表单代码上传CSV文件:
$this->widgetSchema ['file'] = new sfWidgetFormInputFile();
$this->setValidator('file', new sfValidatorFile(array(
'required' => true,
'path' => sfConfig::get('sf_upload_dir') . '/properties/csv',
'mime_categories' => array('csv' => array('text/csv', 'application/csv', 'application/excel', 'application/vnd.ms-excel', 'application/vnd.msexcel')),
'mime_types' => 'csv'
)));
在我的行动中,我有:
if($request->isMethod('post'))
{
$propertyCsvForm->bind($request->getParameter($propertyCsvForm->getName()), $request->getFiles($propertyCsvForm->getName()));
if($propertyCsvForm->isValid())
{
$this->getUser()->setFlash('success-csv', 'The CSV was successfully imported.');
} else {
$this->getUser()->setFlash('error-csv', 'The CSV could not be imported.');
}
}
当我尝试上传CSV时,我总是会收到错误,即mime类型不正确
Invalid mime type (text/plain).
任何想法为什么?
由于
答案 0 :(得分:1)
我有同样的问题,我解决了它创建一个postValidator
我得到了文件的路径,我做了一个测试来检查是否是一个csv文件,它工作正常
class sfValidatorCSV extends sfValidatorSchema
{
protected function configure($options = array(), $messages = array())
{
parent::configure($options, $messages);
}
protected function doClean($values)
{
$file = $values['file'];
if(empty($file)==false)
{
$filename = $file->getOriginalName();
$path = pathinfo($filename);
if($path['extension']!="csv")
{
throw new sfValidatorError($this, 'Invalid extension.');
}
}
}
}
你在表格的末尾称它为:
$this->mergePostValidator(new sfValidatorCSV());