我想在Yii2中添加一个简单的
[a href =“C:/Vo/AGO/2015.pdf”> 2015 [/ a> ([必须是<)
在我的一张表格上。 我不想上传文件,因为pdf(帮助)文件由外部组织更新(而不是C:pad是服务器,但出于测试原因我使用C :),我必须显示该组织管理的大量文件。
所以我用:
Html :: a(“2015”,“C:/Vo/Ago/2015.pdf”)
当我运行应用程序并通过show source进行检查时,我看到了
[a href =“C:/Vo/Ago/2015.pdf”> 2015 [/ a>
但如果我点击表单上的链接,则没有任何反应! (当我在一个简单的html文档中做同样的事情 - 而不是yii2 - pdf打开)
如果我复制右键并复制我得到的链接: 文件:/// C:/Vo/Ago/2015.pdf
那么,我错过了什么? 是的,我是Yii2的新手,我在互联网上搜索了很多,以找到解决方案。
如果已经有人问过这个问题,那么欢迎提及解决方案......
谢谢,
Chris G.M. Logghe
答案 0 :(得分:1)
Because you are trying to link "local" file on browser.
Some browsers, like modern versions of Chrome, will even refuse to cross from the http protocol to the file protocol, so you'd better make sure you open this locally using the file protocol if you want to do this stuff at all.
The best option for you is to create action on controller and perform download file there.
In your view:
$data = 'C:/data/mydata.log';
echo Html::a('Download', ['sample-download', 'filename' => $data], ['target' => '_blank']);
In your controller:
public function actionSampleDownload($filename)
{
ob_clean();
\Yii::$app->response->sendFile($filename)->send();
}
Of course, you must limit to specific directory rather than user give full access to filename.