CSV上载-mime类型



嗨,我正在尝试使用以下表单代码上传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).

有什么想法吗?

感谢

我遇到了同样的问题,我解决了这个问题,创建了一个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());

最新更新