return 在 Yii2 中进程下载后引用或刷新不起作用
return referrer or refresh after process download in Yii2 doesn't work
我想在下载文件后刷新我的页面以更新数据视图,如果我使用变量来处理下载然后返回引荐来源网址,它不起作用。我很困惑,因为我无法使用 return
两次,如何解决?
我在控制器中的代码:
if (file_exists($root)) {
// here's the problem:
return Yii::$app->response->sendFile($root, $profile['resumefilename']); //download resume
return $this->redirect(Yii::$app->request->referrer); // i can't put return here, but i need to refresh page after download
} else {
Yii::$app->session->setFlash('failed', 'Data resume is not found!');
return $this->redirect(Yii::$app->request->referrer);
}
这无法在服务器端完成,限制来自 HTTP 的工作方式,一个请求得到一个响应。
您不能将文件作为 200
响应发送 和 发送 302
重定向响应,它必须是一个 或者其他
由于您在成功和错误时将用户重定向到同一页面,因此从客户端执行相同的操作很容易。
最常见的方法,也可能是最简单的方法,是将 download 属性添加到您的 link:
Html::a('download file',
['file/download', 'id' => $model->id],
['download' => $model->name]
);
这将启动文件下载,但用户不会离开他们所在的页面。
您还可以拦截点击并使用 JavaScript 来管理下载并显示您想要的任何信息、下载进度、错误通知等。
我使用 jquery 找到了我的问题的答案并且有效。我仍然使用 sendFile( )
来处理下载,然后在 js 中添加一些代码。这是我的代码:
//this is my controller
if (file_exists($file)) {
// here's the problem:
return Yii::$app->response->sendFile($file, $profile['resumefilename']);
} else {
Yii::$app->session->setFlash('failed', 'Data resume is not found!');
return $this->redirect(Yii::$app->request->referrer);
}
这里是视图:
<a class="dropdown-item btn-download" href="your_link">Download</a>
这里是 jquery:
$(document).on('click','.btn-download', function() {
setInterval(function(){
location.reload(true);
},1000);
});
我想在下载文件后刷新我的页面以更新数据视图,如果我使用变量来处理下载然后返回引荐来源网址,它不起作用。我很困惑,因为我无法使用 return
两次,如何解决?
我在控制器中的代码:
if (file_exists($root)) {
// here's the problem:
return Yii::$app->response->sendFile($root, $profile['resumefilename']); //download resume
return $this->redirect(Yii::$app->request->referrer); // i can't put return here, but i need to refresh page after download
} else {
Yii::$app->session->setFlash('failed', 'Data resume is not found!');
return $this->redirect(Yii::$app->request->referrer);
}
这无法在服务器端完成,限制来自 HTTP 的工作方式,一个请求得到一个响应。
您不能将文件作为 200
响应发送 和 发送 302
重定向响应,它必须是一个 或者其他
由于您在成功和错误时将用户重定向到同一页面,因此从客户端执行相同的操作很容易。
最常见的方法,也可能是最简单的方法,是将 download 属性添加到您的 link:
Html::a('download file',
['file/download', 'id' => $model->id],
['download' => $model->name]
);
这将启动文件下载,但用户不会离开他们所在的页面。
您还可以拦截点击并使用 JavaScript 来管理下载并显示您想要的任何信息、下载进度、错误通知等。
我使用 jquery 找到了我的问题的答案并且有效。我仍然使用 sendFile( )
来处理下载,然后在 js 中添加一些代码。这是我的代码:
//this is my controller
if (file_exists($file)) {
// here's the problem:
return Yii::$app->response->sendFile($file, $profile['resumefilename']);
} else {
Yii::$app->session->setFlash('failed', 'Data resume is not found!');
return $this->redirect(Yii::$app->request->referrer);
}
这里是视图:
<a class="dropdown-item btn-download" href="your_link">Download</a>
这里是 jquery:
$(document).on('click','.btn-download', function() {
setInterval(function(){
location.reload(true);
},1000);
});