django文件上传下载

 

 

def update(req: django.http.HttpRequest):
    if req.method == "POST":
        print("has file")
        with open("f:/file", "wb") as f:
            print("get f")
            print(req.FILES)
            for i in req.FILES["file_name"].chunks():
                f.write(i)
    return render(req, "first/index.html")

# 使用流式传输
def download_bigfile(request):
    file_name = 文件名

    def openFile(fileName):
        with open(fileName, "rb") as f:
            while True:
                c = f.read(1024)
                if c:
                    yield c
                else:
                    break

    response = StreamingHttpResponse(openFile(file_name))
    # 要指定 http 头
    response['Content-Type'] = 'application/octet-stream'
    response['Content-Disposition'] = 'attachment;filename="{0}"'.format(file_name)
    return response

 

posted @ 2019-09-16 17:01  651635  阅读(243)  评论(0编辑  收藏  举报