【django】后台上传于下载 (通用型下载文件的 路由与下载函数)

【django】后台上传于下载 (通用型下载文件的 路由与下载函数),第1张

概述通用路由: # 下载文件 url("media/(?P<path>.*)$", file_down.FileDown.as_view()) 通用下载文件函数: from
通用路由:     # 下载文件    url("media/(?P<path>.*)$",file_down.fileDown.as_vIEw())通用下载文件函数:    from django.utils.http import urlquote rest_framework.vIEws import APIVIEw django.shortcuts import render,redirect,httpResponse dal import models django.http import JsonResponse,fileResponse,StreaminghttpResponseimport osimport xlwtBASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # C:\Users\user\Desktop\DownTestclass fileDown(APIVIEw):    def get(self,request,path):        path = BASE_DIR + \media\" + path.replace(/",\)        print(path {}        file_name = path.split(")[-1]        print(file_name" ""),1)">'rb')  # 字符串替换成文件        # 告诉浏览器 这个是下载文件        response = fileResponse(file)        response[Content-Type'] = application/octet-stream        response[Content-dispositionattachment;filename={}.format(urlquote(file_name))  # 设置名字        print(response)        return response
通用settings配置:LANGUAGE_CODE = zh-hansTIME_ZONE = Asia/ShanghaiUSE_I18N = TrueUSE_L10N = TrueUSE_TZ = True# Internationalization# https://docs.djangoproject.com/en/1.11/topics/i18n/STATIC_URL = /static/STATICfileS_Dirs = [    os.path.join(BASE_DIR,static),os.path.join(BASE_DIR,1)">media/media/# media配置,用户上传的文件都默认放在这个文件夹下MEDIA_ROOT = os.path.join(BASE_DIR,1)">)REST_FRAMEWORK = {    DEFAulT_AUTHENTICATION_CLASSES: [],1)">DEFAulT_PERMISSION_CLASSES数据库设计: Suggestion(models.Model):    """     项目建议表    """    name = models.ForeignKey(to=UserInfo建议人员)    file = models.fileFIEld(upload_to=suggestion/files',null=False,unique=True,blank=False,1)">文件(点击下载))    file_name = models.CharFIEld( max_length=50,1)">文件标题)    is_staff =models.BooleanFIEld(verbose_name=是否紧急)    is_staf =models.BooleanFIEld(verbose_name=是否采用)    def __str__(self):        return self.file     Meta:        verbose_name = 项目建议表        verbose_name_plural = verbose_name        db_table = Suggestion'

 

总结

以上是内存溢出为你收集整理的【django】 后台上传于下载 (通用型下载文件的 路由与下载函数)全部内容,希望文章能够帮你解决【django】 后台上传于下载 (通用型下载文件的 路由与下载函数)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1222804.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存