Django文件下载,解决Django中文名问题

来源:互联网 发布:linux下ant的安装配置 编辑:程序博客网 时间:2024/05/29 15:31

支持大文件下载和Django文件中文名字


#iterator



def readFile(file_name):
    f= open(file_name,'rb')
    while True:
        c= f.read(1024)
        if c:
            yield c
        else:
            break;


#down file 
def down_File(request):
    try:
        file_name ='cfdjfdf008好好tf.mp4'
        response = StreamingHttpResponse(readFile('D:/'+file_name))
        response['mimetype']='application/octet-stream'
        response['Content-Type']='text/plain'
    #     response['Content-Disposition']='attachment; filename*={}'.format(escape_uri_path(file_name))
        response['Content-Disposition'] = "attachment; filename*=utf-8''{}".format(escape_uri_path(file_name))
        return response;   
    except:
        return HttpResponse("failed!please try it again!")
   
0 0
原创粉丝点击