zoukankan      html  css  js  c++  java
  • django 上传和下载文件

    
    
    前端直接<a :href="'http://127.0.0.1:8000/download_excel/?id='+ fileId">下载</a>
    

      

    ##上传文件
    #setting配置
     UPLOAD_ROOT = os.path.join(BASE_DIR,'upload')
    from django.http import JsonResponse,HttpResponse from WorkFlowsys.settings import UPLOAD_ROOT order_number = lambda : int(round(time.time()* 1000*1000)) ##避免重复文件 if fp.multiple_chunks(): # 判断上传文件大于2.5MB的大文件 file_yield = fp.chunks() # 迭代写入文件 with open(path,'wb') as f: for buf in file_yield: f.write(buf) else: with open(path,'wb') as f: f.write(fp.read()) ##文件下载 class Upload_file(APIView): def get(self,request): id =request.GET.get('id') query = Question.objects.filter(id = id).first() filename = query.accessory with open (filename,"rb") as file_object: response = HttpResponse(file_object) response['Content-Type'] = 'application/octet-stream' #设置头信息,告诉浏览器这是个文件 filename = 'attachment; filename=%s'%filename.split("\")[6] #设置文件名的中文编码方式 response['Content-Disposition'] = filename.encode('utf-8', 'ISO-8859-1') return response

      

      

  • 相关阅读:
    运行python脚本后台执行(转载)
    双积分式(A/D)转换器电路结构及工作原理
    SAR ADC简介
    [hdu-6395]Sequence 分块+矩阵快速幂
    [hdu-1272]小希的迷宫 并查集
    redhat7.2安全基线BI
    Oracle 12c RAC
    .gitlab-ci.yml简介
    gitlab
    Centos 7 Docker安装配置
  • 原文地址:https://www.cnblogs.com/sunzhiqi/p/14115641.html
Copyright © 2011-2022 走看看