ThinkPHP框架下有个Org/Net/Http.class.php的方法download。

Http.class.php的download方法如下

/**
* 下载文件
* 可以指定下载显示的文件名,并自动发送相应的Header信息
* 如果指定了content参数,则下载该参数的内容
* @static
* @access public
* @param string $filename 下载文件名
* @param string $showname 下载显示的文件名
* @param string $content 下载的内容
* @param integer $expire 下载内容浏览器缓存时间
* @return void
*/
static public function download ($filename, $showname='',$content='',$expire=180) {
if(is_file($filename)) {
$length = filesize($filename);
}elseif(is_file(UPLOAD_PATH.$filename)) {
$filename = UPLOAD_PATH.$filename;
$length = filesize($filename);
}elseif($content != '') {
$length = strlen($content);
}else {
E($filename.L('下载文件不存在!'));
}
if(empty($showname)) {
$showname = $filename;
}
$showname = basename($showname);
if(!empty($filename)) {
$finfo = new \finfo(FILEINFO_MIME);
$type = $finfo->file($filename);
}else{
$type = "application/octet-stream";
}
//发送Http Header信息 开始下载
header("Pragma: public");
header("Cache-control: max-age=".$expire);
//header('Cache-Control: no-store, no-cache, must-revalidate');
header("Expires: " . gmdate("D, d M Y H:i:s",time()+$expire) . "GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s",time()) . "GMT");
header("Content-Disposition: attachment; filename=".$showname);
header("Content-Length: ".$length);
header("Content-type: ".$type);
header('Content-Encoding: none');
header("Content-Transfer-Encoding: binary" );
if($content == '' ) {
readfile($filename);
}else {
echo($content);
}
exit();
}

首先需要看一下大家使用的Thinkphp的版本,不同的版本使用的方法不同,(在导入公共函数的时候方式不同)

我用的是thinkphp3.2.3版本的,因此直接使用import()函数,直接把使用thinkphp自带的http类的功能实现下载。但是在引用公共类的时候,

首先需要使用import来导出公共类,import('ORG.Net.Http');。接下来最重要的是需要重新new一下,否则无法使用类。$http = new \Org\Net\Http();

然后接下来就需要把需要下载的文件路径注明。(必须使用绝对路径)。

我写的代码如下:

/*
* 下载文件
*/
public function download(){
if(IS_GET){
$id=i('id');
$res=D('Activities')->where("id='{$id}'")->find();
import('ORG.Net.Http');
$http = new \Org\Net\Http();
$http->download($res['attachment']);//数据库存的绝对路径
}else{
$this->ajaxReturn(['info'=>'非法请求','status'=>0]);
}
}

thinkphp,下载附件的更多相关文章

  1. Firefox下载附件乱码的解决办法

    通过在http的header里设置fileName下载附件时,中文文件名通过chrome浏览器下载时正常,通过firefox下载时为乱码: 原来的Java代码: response.addHeader( ...

  2. Nginx 配置下载附件让浏览器提示用户是否保存

    Nginx配置下载附件让浏览器提示用户是否保存   by:授客  QQ:1033553122   测试环境 nginx-1.10.0 问题描述: 前端页面,IE11浏览器下请求下载附件模板,针对xls ...

  3. java上传附件,批量下载附件(一)

    上传附件代码:借助commons-fileupload-1.2.jar package com.str; import java.io.BufferedInputStream;import java. ...

  4. WebApi下载附件文件

    WebApi下载附件文件 1. [RoutePrefix("down")] public class FilesController : ApiController { [GET( ...

  5. vue+springboot上传和下载附件功能

    https://blog.csdn.net/qq_35867245/article/details/84325385 上传附件(服务端代码) 第一步:在application.yml中配置附件要上传的 ...

  6. JDBC中级篇(MYSQL)——模拟从数据库中上传下载附件

    注意:其中的JdbcUtil是我自定义的连接工具类:代码例子链接: package b_blob_clob; import java.io.BufferedOutputStream; import j ...

  7. 通过HttpURLConnection下载图片到本地--下载附件

    一.背景说明 现在我做的系统中,需要有一个下载附件的功能,其实就是下载图片到本地中.相应的图片保存在多媒体系统中,我们只能拿到它的资源地址(url),而不是真实的文件. 这里记录的是下载单个图片.下篇 ...

  8. vue Blob 下载附件报错

    vue Blob 下载附件报错,不妨试试: window.location.href=后台地址

  9. 修改nginx配置文件解决dx2.5下载附件停止不动的问题

    在下载论坛附件的时候,总是停止在某个字数数不动 如下图 后来查看log发现 如下图 权限拒绝 发现后nginx的配置文件的启动者有关系 改了下 user 为 root 居然好了

随机推荐

  1. hadoop2.7.0实践- WordCount

    环境要求 说明:本文档为wordcount的mapreduce job编写及执行文档. 操作系统:Ubuntu14 x64位 Hadoop:Hadoop 2.7.0 Hadoop官网:http://h ...

  2. atitit.提升研发效率的利器---重型框架与类库的区别与设计原则

    atitit.提升研发效率的利器---重型框架与类库的区别与设计原则 1. 框架的意义---设计的复用 1 1.1. 重型框架就是it界的重武器. 1 2. 框架 VS. 库 可视化图形化 1 2.1 ...

  3. Atitit.  单列索引与多列索引 多个条件的查询原理与设计实现

    Atitit.  单列索引与多列索引 多个条件的查询原理与设计实现 1. MySQL只能使用一个索引1 1.1. 最左前缀1 1.2. 从另一方面理解,它相当于我们创建了(firstname,last ...

  4. Yii2数据库查询语法

    一: $con = Yii::$app->db; $rel = $con->createCommand("select * from user");//预处理对象 $r ...

  5. 85. Insert Node in a Binary Search Tree【easy】

    Given a binary search tree and a new tree node, insert the node into the tree. You should keep the t ...

  6. iptables简单配置

    iptables简单配置 分类: Linux 安全2010-10-20 16:56 4241人阅读 评论(0) 收藏 举报 input防火墙tcpfilterubuntuservice # iptab ...

  7. vi/vim 光标移动命令

    vi/vim 光标移动命令 移动光标上:k nk:向上移动n行 9999k或gg可以移到第一行 G移到最后一行下:j nj:向下移动n行左:h nh:向左移动n列右:l nl:向右移动n列 w:光标以 ...

  8. hadoop单节点配置

    首先按照官网的单机去配置,如果官网不行的话可以参考一下配置,这个是配置成功过的.但是不一定每次都成功 http://hadoop.apache.org/docs/r2.6.5/ centos 6.7 ...

  9. 原生javascript 实现的文本编辑器

    直接来干活上代码.就一个函数execCommand():自己百度一下. 在线预览 <!doctype html> <html> <head> <title&g ...

  10. C#高级学习群欢迎你(群号 128874886)

    C#高级学习群,有着C# ,Asp.net ,Wpf等技术经验相当丰富的工程师,秉承着刘群主开源共享的精神,为新手和高手们提供了良好的学习交流平台,自创群以来,为群员解决了不少的技术难题,大大提高了学 ...