0

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

任何想法为什么?

谢谢

4

1 回答 1

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());
于 2013-05-30T10:21:21.767 回答