【问题标题】:Yii2 sendFile() - Trying to get property of non-objectYii2 sendFile() - 试图获取非对象的属性
【发布时间】:2016-01-21 05:03:11
【问题描述】:

大家好,我正在尝试设置一个下载按钮,用户可以在其中下载文件,该文件是使用 Kartik 的文件输入小部件(单次上传)上传的。

代码位于 CRUD 生成的视图/控制器/模型中。

这是查看按钮代码,

        <?= Html::a('Download Uploaded File', ['download', 'id' => $model->form_id], [
    'class' => 'btn btn-danger',
    'data' => [
        'confirm' => 'Are you sure you want to download this item?',
        'method' => 'post',
    ],
]) ?>

控制器功能(下载),

public function actionDownload($id) {
    $model = $this->findModel($id);

   $path = Yii::getAlias('@web') . '/uploads';
   $file = '/borang/'.$model->form_id.'.'.$model->file->extension;

   if (file_exists($file)) {

   Yii::$app->response->sendFile($file);

  }
}

控制器功能(上传在创建动作中)

public function actionCreate()
    {   

        $model = new FormMovement();

        if ($model->load(Yii::$app->request->post())) {

            $model->file = UploadedFile::getInstance($model, 'file');

            if (!empty($model->file) && $model->validate()) {
                    $model->fm_upload = 'uploads/borang/'.$model->form_id.'.'.$model->file->extension;
                    $model->save();
                    $model->file->saveAs('uploads/borang/'.$model->form_id.'.'.$model->file->extension);
                    return $this->redirect(['view', 'id' => $model->form_id]);
            }else{
                $model->save();
                return $this->redirect(['view', 'id' => $model->form_id]);
            }

        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

这是错误日志, C:\xampp\htdocs\adminsys\frontend\controllers\FormMovementController.php:181 中的“试图获取非对象的属性”

哪个指向,

$file = '/borang/'.$model->form_id.'.'.$model->file->extension;

在下载动作(控制器)内

【问题讨论】:

  • 这部分$model-&gt;file-&gt;extension在下载操作中找不到。

标签: php yii2


【解决方案1】:

试试这个方法:

public function actionDownload($id) {
    $model = $this->findModel($id);

    $path = Yii::getAlias('@web') . '/uploads';
    $ext = substr(strrchr($model->file,'.'),1);
    $file = $path.$model->file;
    $download = '/borang/'.$model->form_id.'.'.$ext;
    if(file_exists($file)) 
       Yii::$app->response->sendFile($download);
}

strrchr()

【讨论】:

  • 我尝试了代码,没有返回任何错误,但是我得到了一个没有内容的白页。我将重定向命令放到“查看”页面,现在它只是刷新页面。意思是下载链接不起作用,我猜。
【解决方案2】:

问题在于您的下载功能。您没有在模型中保存扩展名。获取文件扩展名的方法有很多。

public function actionDownload($id) {
    $model = $this->findModel($id);

   $path = Yii::getAlias('@webroot') . '/uploads';
   $fileextension=end(explode('.',$model->fm_upload));
   $file = $path.'/borang/'.$model->form_id.'.'.$fileextension;

   if (file_exists($file)) {

   Yii::$app->response->sendFile($file);

  }
}

【讨论】:

  • 就像我对 Insane Skull 的方法所做的那样,尝试了您的代码并得到了相同的结果。没有返回错误,但我得到一个没有内容的白页。我将重定向命令放到“查看”页面,现在它只是刷新页面。意思是下载链接不起作用,我猜。
  • 我已将uplaod文件夹路径更改为“ $path = Yii::getAlias('@webroot') . '/uploads';” .检查我的更新答案。
  • 有效!我不能同时选择两个作为接受的答案,但我选择了我最了解的一个。谢谢!
【解决方案3】:

您需要确保在服务器上启用了 set_time_limit 以使下载选项正常工作,因为默认情况下它将在服务器上禁用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多