首页 > 编程知识 正文

简述如何实现文件,vue如何实现文件

时间:2023-05-06 20:16:52 阅读:263428 作者:2122

需求:把一个文件,对外提供url路径,来完成文件下载。

 修改nginx配置文件

  先看配置文件

server { listen 80; server_name localhost; location ^~ /download/ { alias /home/webhtml/; if ($request_filename ~* ^.*?.(html|doc|pdf|zip|docx|txt)$) { add_header Content-Disposition attachment; add_header Content-Type application/octet-stream; } sendfile on; # 开启高效文件传输模式 autoindex on; # 开启目录文件列表 autoindex_exact_size on; # 显示出文件的确切大小,单位是bytes autoindex_localtime on; # 显示的文件时间为文件的服务器时间 charset utf-8,gbk; # 避免中文乱码 }}

 

 关键点解析:

  其中 

server_name  localhost;  代表服务名,在这里直接访问 127.0.0.1就可以。如果是别的机器访问这台机器。则直接替换成这台机器的地址即可。

 location ^~ /download/ 带边的是拦截包含 download的请求

 alias /home/webhtml/;  是来配置下载文件的路径,也就是提供下载的文件要在这个路径下。

  if ($request_filename ~* ^.*?.(html|doc|pdf|zip|docx|txt)$) {
            add_header Content-Disposition attachment;
            add_header Content-Type application/octet-stream;
        }

这段代码,会去判断文件的后缀。以及提供下载功能。

修改完配置文件重启nginx即可。这里不提供命令了。我是用docker的形式,比较简单。直接一个 docker restar nginx就行了。不过我提供一片文章:nginx重启几种方法 - 习惯沉淀 - 博客园

使用例子

   首先把一个待下载的文件放在:/home/webhtml/ 目录下

  例如:

然后就可以用这个url来下载文件了(本机访问): 127.0.0.1/download/hello.txt

一般都是对外提供下载的(假设我这台部署nginx的机器是:10.2.5.10):10.2.5.10/download/hello.tx  

我们可以知道的是,带有download的路径被拦截了,加上我们配的文件路径,最后去取文件:

/home/webhtml/hello.txt

 

  

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。