微信公众号里如何实现 点击下载 的功能,服务器端为php

在后台上传一个文件(.doc、.docx、.pdf、.rar、.zip格式),前端用ajax请求获取该文件路径,然后click事件中实现下载

阅读 5.3k
1 个回答

不需要Ajax 直接<a href='http://example.com/test.doc' download>下载</a>

或者点击链接,php下载

$file  =  'test.doc' ;

if ( file_exists ( $file )) {
     header ( 'Content-Description: File Transfer' );
     header ( 'Content-Type: application/octet-stream' );
     header ( 'Content-Disposition: attachment; filename=' . basename ( $file ));
     header ( 'Content-Transfer-Encoding: binary' );
     header ( 'Expires: 0' );
     header ( 'Cache-Control: must-revalidate' );
     header ( 'Pragma: public' );
     header ( 'Content-Length: '  .  filesize ( $file ));
     ob_clean ();
     flush ();
     readfile ( $file );
    exit;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题