自助点击<a> element inside function

时间:2016-07-05 20:40:00

标签: javascript php yii2

I have to download a file from server and I have this function.

private function downloadFile($dir,$file,$alias)
{    
    $path=$dir.$file;
    $name=$alias.".pptx";
    $size = filesize($path);
    header("Content-Type: application/force-download");
    header("Content-Disposition: attachment; filename='$name'");
    header("Content-Transfer-Encoding: BINARY");
    header("Content-Length: " . $size);
    // Descargar archivo
    readfile($path);
    Yii::$app->session->setFlash("errordownload");
    exit;

But I would like to know if its possible use a self-click html element instead of headers.

Something like this

private function downloadFile($dir,$file,$alias)
{    
    echo '<a id="a" href="'.$dir.$file.'" download="'$alias.".pptx".'" class=" btn btn-primary">Descargar Archivo</a>';

    echo'<script type="text/javascript"> 
            $(document).ready(function(){
               $("#a").click();
            }
          </script>'

}

I want to do it through <a> because otherwise I get some errors when I try to open the file. The file in server opens with no errors.

0 个答案:

没有答案