yii2 返回json和文件下载

JSON

調用的控制器返回 json格式的數據即可,對json裡面的數據沒有要求

如在控制器中添加一個方法:

public function actionRemoveImage($id){
    Yii::$app->response->format = yii\web\Response::FORMAT_JSON;
    Yii::$app->response->data = [
        'status' => true
    ];
}

yii2 下載文件

使用 yii2的sendfile方法

function actionDownload()
{
    $imgFullPath = 'picture.jpg';
    return Yii::$app->response->sendFile($imgFullPath);
}
References
  1. How add one more button to yii2-widget-fileinput?

yii2 file input 如果需要有移除效果,要配置initialPreviewConfig 參數

posted @ 2019-07-28 17:56  五毛钱的饼  阅读(1378)  评论(0编辑  收藏  举报