2016-10-29 2 views
0

Ich bin sehr neu mit YII2. Ich möchte eine Download-Funktion für die Datei erstellen, die zuvor hochgeladen wurde. Ich habe bereits erwähnt, wie man einen Action-Download bei Gridview in Yii2 erstellt. Wenn ich jedoch auf den Download der Schaltfläche klicke, wird die leere Seite geladen. Hier der Code.Aktion download in gridview yii2

Bei gridview

<?=GridView::widget([ 
     'dataProvider'=>$dataProvider, 
     'id'=>'mygrid', 
     'columns' => [ 
     ['class' => 'yii\grid\SerialColumn'], 
     'project_name', 
     'project_file', 
     'created_date', 
     [ 
     'class' => 'yii\grid\ActionColumn', 
     ], 
     ['attribute'=>'Download', 
     'format'=>'raw', 
     'value' => function($data) 
     { 
     return 
     Html::a('Download file', ['firstyear/download', 'id' => $data->id],['class' => 'btn btn-primary']); 

     } 
     ], 
      ] 

]); ?>

Bei actionDownload

public function actionDownload($id) 
{ 
$download = Firstyear::findOne($id); 
$path=Yii::getAlias('@webroot').'/uploads/'.$download->project_file; 
if (file_exists($path)) { 

    return Yii::$app->response->sendFile($path); 

} 
} 

Bei actionCreate (Upload-Datei)

public function actionCreate() 
{ 
    $model = new Firstyear(); 
    if ($model->load(Yii::$app->request->post())) 
    {  
      $project =$model->project_name; 
      $model->file= UploadedFile::getInstance($model,'file'); 
      $model-> file->saveAs('uploads/'.$project.'.'.$model->file->extension); 
      $model->project_file='uploads/'.$project.'.'.$model->file->extension; 

      $model->save(); 
      Yii::$app->getSession()->setFlash('success','Data saved!'); 
      return $this->redirect(['view','id'=> $model->id]); 
      } 

      else { 

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

} 

Dank.

Antwort

2

Ich vermute, dass der $ Pfad nicht korrekt ist, in der Aktion zu erstellen, Sie enthalten bereits die Uploads Ordner in Ihrem $ modell-> project_file

public function actionCreate() 
{ 
    $model = new Firstyear(); 
    if ($model->load(Yii::$app->request->post())) 
    {  
      ....... 
      $model->project_file='uploads/'.$project.'.'.$model->file->extension; 
      $model->save(); 
      .... 
    }   

} 

aber Sie es verwenden, wieder in actionDownlaod

public function actionDownload($id) 
{ 
    ..... 
    $path=Yii::getAlias('@webroot').'/uploads/'.$download->project_file; 
    if (file_exists($path)) { 
     return Yii::$app->response->sendFile($path); 

    } 
} 

Sie sollten versuchen, die uploads Ordner im Akt zu entfernen ionDownlaod und fügte einige Debug-Nachricht

public function actionDownload($id) 
    { 
     ..... 
     $path=Yii::getAlias('@webroot').'/'.$download->project_file; 
     if (file_exists($path)) { 
      return Yii::$app->response->sendFile($path); 
     } else { 
      throw new NotFoundHttpException("can't find {$download->project_file} file"); 
     } 
    } 
+0

Dank David .. Danke so much..Great! Es klappt! @David – Fyp16

+0

Gern geschehen. Ich bin froh, dass es funktioniert hat – David