We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Ajax phalcon php force file download

My download file path is in application/app/upfiles

$this->view->setRenderLevel(View::LEVEL_NO_RENDER);
    $request = new Phalcon\Http\Request();
    if ($request->isPost()) {
      if ($request->isAjax()) {
        $fileid = $this->request->getPost('downloadpdf'); //get upload id from ajax post
        $download = Upload::findFirst("upload_id = '$fileid'");
        $filename = $download->upload_file;

        $response = new \Phalcon\Http\Response();
        $path = "app/upfiles/" . $filename;
        $this->flash->error($path);
        $filetype = filetype($path);
        $filesize = filesize($path);
        $response->setHeader("Cache-Control", 'must-revalidate, post-check=0, pre-check=0');
        $response->setHeader("Content-Description", 'File Download');
        $response->setHeader("Content-Type", 'octet-stream');
        $response->setHeader("Content-Length", $filesize);
        $response->setFileToSend($path, str_replace(" ","-",$filename), true);
        $response->send();
        die();
      }
    }

But not downloading, please any one resolve it

Hi @bhanukrish it isn't a problem with Phalcon, it's a problem with browsers

You can use download attribute or read this post

Good luck