0

I want in Yii2 a simple

[a href="C:/Vo/AGO/2015.pdf">2015 [/a> ([ must be a <)

on one of my forms. I don't want to upload the file, because the pdf (help) file is updated by an external organisation (instead of C: the pad is a server, but for test reasons I use C:), and I have to display a lot of files managed by that organisation.

So I use:

Html::a("2015", "C:/Vo/Ago/2015.pdf")

When I run the application and I inspect via show source I see

[a href="C:/Vo/Ago/2015.pdf">2015[/a>

But if I click the link on my form, nothing happens! (When I do the same thing in a simple html document - not yii2 - the pdf opens)

If I copy right-click and copy the link I get: file:///C:/Vo/Ago/2015.pdf

So, what am I missing? Yes I'am new in Yii2 and I searched a lot on internet to find a solution.

If this is already asked, excuse me, a reference to the solution would then be welcome...

Thanks,

Chris G.M. Logghe

loggheci
  • 1
  • 2
  • Even if your code is working on your machine, it will not work on server, because you are trying to access the file locally. – MrD Apr 07 '16 at 12:13

1 Answers1

0

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.

See here for more details.

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.

Community
  • 1
  • 1
agungandika
  • 166
  • 9