下载接口:

服务端flask下载接口

@app.route("/api/download/", methods=["POST"])
def download():
try:
logger.debug("download start")
param = request.get_json(force=True).get('param')
logger.debug("download({})".format(param))
file_path = param.get('file_path')
file_name = param.get('file_name')
base_dir = u"C:\\Program Files\\download"
file_dir = os.path.join(base_dir, file_path)
file_abs_path = os.path.join(file_dir, file_name)
logger.debug("send_from_directory file_dir ({}) ,file_name({})".format(file_dir, file_name))
response_file = send_from_directory(file_dir, filename=file_name, as_attachment=False)
response = make_response(response_file)
response.headers["Content-Disposition"] = "attachment; filename={}".format(file_name.encode().decode("latin-1"))
response.headers["Content-Length"] = os.stat(file_abs_path).st_size
response.headers["Content-Type"] = "application/octet-stream"
return response
except:
logger.debug("download failed ({})".format(traceback.format_exc()))
response = {"status": 9999}
return jsonify(response)

客户端下载接口

url = "https://{}:{}/api/download/".format(server_ip, get_https_port())
data = {"param": {
"file_path": file_path,
"file_name": file_name
}}
save_to = "/home" try:
logger.debug("download, url[%s] data:%s" % (url, data))
   download_headers = {"Content-Type": "application/json"}
res = requests.post(url=url, headers=download_headers, data=json.dumps(data), verify=False)
logger.debug('download response status[%s] headers:%s' % (res.status_code, res.headers)) if not os.path.exists(save_to):
os.makedirs(save_to) save_path = os.path.join(save_to, file_name) with open(save_path, "wb") as fp:
for chunk in res.iter_content(1024):
if not chunk:
break
fp.write(chunk)

上传接口:

服务接口上传文件接口

@app.route("/api/upload/", methods=["POST"])
def upload():
if request.method == "POST":
try:
logger.debug("upload vars ({})".format(request.files))
file = request.files['file']
content = file.read()
# bdecode(content) path = "/home"
file_path = os.path.join(path, file_name)
if os.path.exists(file_path):
os.remove(file_path) with open(file_path, "wb") as f:
f.write(content)
f.flush()
f.close()
return jsonify({"status": 0, "data": []})
except:
logger.debug("save upload file fail ({})".format(traceback.format_exc()))
return jsonify({"status": 1, "data": []})

客户端上传接口:

url = "https://{}:{}/api/download/".format("192.168.11.200", "8000")
file_path = "/home/dddd/aaa.txt"
file_name = os.path.split(file_path)[-1]
send_data = {}

headers = {
'Connection': 'keep-alive',
'User-Agent': 'P2pclient with Linuxos',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate',
}

if not send_data:
with open(file_path, 'rb')as fp:
file_list = {'file': (file_name, fp, ''), "uuid": timestamp_uuid, "sign": sign,
"Authorization": "token"}
response = requests.post(url, headers=headers, timeout=15,
files=file_list, verify=False)
else:
with open(file_path, 'rb')as fp:
file_list = {'file': (file_name, fp, ''), "uuid": timestamp_uuid, "sign": sign,
"Authorization": "token"}
response = requests.post(url, headers=headers, timeout=15,
data=send_data, files=file_list, verify=False)
logger.debug('upload {} to {} response {}'.format(file_path, url, response.__dict__))
data = response.json()
if data.get('status') == 0:
logger.debug('upload {} to {} successed!'.format(file_path, url))
return True
else:
logger.debug('upload {} to {} failed, try again ... {}'.format(file_path, url, i))
return False

Falsk 大文件上传/下载(send_from_directory)的更多相关文章

  1. Java实现FTP批量大文件上传下载篇1

    本文介绍了在Java中,如何使用Java现有的可用的库来编写FTP客户端代码,并开发成Applet控件,做成基于Web的批量.大文件的上传下载控件.文章在比较了一系列FTP客户库的基础上,就其中一个比 ...

  2. java+大文件上传下载

    文件上传下载,与传统的方式不同,这里能够上传和下载10G以上的文件.而且支持断点续传. 通常情况下,我们在网站上面下载的时候都是单个文件下载,但是在实际的业务场景中,我们经常会遇到客户需要批量下载的场 ...

  3. java+web+大文件上传下载

    文件上传是最古老的互联网操作之一,20多年来几乎没有怎么变化,还是操作麻烦.缺乏交互.用户体验差. 一.前端代码 英国程序员Remy Sharp总结了这些新的接口 ,本文在他的基础之上,讨论在前端采用 ...

  4. java 如何实现大文件上传下载(传输)各种格式

    我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 首先我们需要了解的是上传文件三要素: 1.表单提交方式:post (get方式提交有大小 ...

  5. HTML上传文件支持大文件上传,下载

    上传 1.修改配置文件web.config,在<system.webServer>下面加入 <security> <requestFiltering > <r ...

  6. java+大文件上传+下载

    我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 这次项目的需求: 支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,i ...

  7. html大文件上传下载

    一.概述 所谓断点续传,其实只是指下载,也就是要从文件已经下载的地方开始继续下载.在以前版本的HTTP协议是不支持断点的,HTTP/1.1开始就支持了.一般断点下载时才用到Range和Content- ...

  8. 全网最简单的大文件上传与下载代码实现(React+Go)

    前言 前段时间我需要实现大文件上传的需求,在网上查找了很多资料,并且也发现已经有很多优秀的博客讲了大文件上传下载这个功能. 我的项目是个比较简单的项目,并没有采用特别复杂的实现方式,所以我这篇文章的目 ...

  9. Nginx集群之WCF大文件上传及下载(支持6G传输)

    目录 1       大概思路... 1 2       Nginx集群之WCF大文件上传及下载... 1 3       BasicHttpBinding相关配置解析... 2 4       编写 ...

  10. PHP实现大文件上传和下载

    一提到大文件上传,首先想到的是啥??? 没错,就是修改php.ini文件里的上传限制,那就是upload_max_filesize.修改成合适参数我们就可以进行愉快的上传文件了.当然啦,这是一般情况下 ...

随机推荐

  1. [Nginx]status:203 Failed to start The NGINX HTTP and reverse proxy server

    怎么感觉Linux的nGinx比Win的事一个一个一个的多啊(半恼) 运行systemctl status nginx时提示: ① Process: 123456 ExecStartPre=/usr/ ...

  2. k8s pod 抓包

    首先安装tcpdump: yum install tcpdump kubectl get pod -o wide查看pod在哪个节点上 docker ps 查看container的id 查看pid: ...

  3. Plus 3.0 (ThinkSNS+)centos8.5+php7.4在阿里云部署过程

    参考:https://zhiyicx.github.io/ts-api-docs/guide/installation/using-nginx-and-fpm-publish-website.html ...

  4. vue组件之间的传参

    vue组件之间传参有三种传参方式'父传子','子传父','非父子组件之间传值' 父传子 父组件 <template> <CounterCom :num="5"&g ...

  5. ANT+JMETER+Jenkins 接口自动化

    Linux环境下搭建:ANT+JMETER+Jenkins 接口自动化 一.准备环境: 1.下载 JDK1.8 JDK下载地址:https://www.oracle.com/java/technolo ...

  6. Vue3+vite+Echarts案例大屏可视化--千峰(推荐)

    https://www.bilibili.com/video/BV14u411D7qK?p=33&spm_id_from=pageDriver&vd_source=e2cfe74d93 ...

  7. PC端 图片宽度是百分比,动态设置图片高度为 6:9

    我们知道图片宽度可以设置  百分比,但是高度要给一个固定值  不然不生效,并且产品要求图片显示必须是9:6,这开始确实难倒我了 后面想了一下用js  获取图片宽度  动态的计算高度就行了,超简单 se ...

  8. Job for nfs-server.service failed because the control process exited with error code. See "systemctl status nfs-server.service" and "journalctl -xe" for details.

    问题: 解决:

  9. Docker-Compose实战<下篇>

    本文是在上一篇文章的基础上做的一些内容追加,上文最后截止内容是docker-compose build将镜像生成完成.接下来我将继续写启动相关服务,访问服务以及常用命令使用等. 1 启动镜像 使用命令 ...

  10. NIO 缓冲区 ByteBuffer 之黏包和半包

    一.低效率方式 /** * 黏包.半包 */ private static void buffExample2() { /* 网络上传输多条数据给服务器,数据之间使用 \n 分隔. 但由于某种原因(多 ...