美文网首页
实现文件的下载

实现文件的下载

作者: 不咸的Yan | 来源:发表于2019-08-21 22:01 被阅读0次
report = CustomHistoryReport.objects.filter(id=reportid)
    if report:
        abs_path = REPORT_PATH + report[0].name + '.xlsx'
        if not os.path.isfile(abs_path):  # 判断下载文件是否存在
            return Response(status=404)
        else:
            with open(abs_path, 'rb') as report:
                file_content = report.read()
            res = file_content
            response = HttpResponse()
                       #声明流数据的类型
            response['Content-Type'] = 'application/octet-stream'
                        #将流数据写入到相应对象中
            response.write(res)
            return response

相关文章

网友评论

      本文标题:实现文件的下载

      本文链接:https://www.haomeiwen.com/subject/fnswsctx.html