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.
Dank David .. Danke so much..Great! Es klappt! @David – Fyp16
Gern geschehen. Ich bin froh, dass es funktioniert hat – David