美文网首页
Element按钮下载文件(后端Python)

Element按钮下载文件(后端Python)

作者: kentlin | 来源:发表于2019-07-05 18:18 被阅读0次

HTML

<el-button class="btns" size='mini' type="success" @click='downloadFun'>下载</el-button>

JS

 //data
    fileUrl: ''
 //methods
    downloadFun() {
        location.href = site_url + 'file_down/?url=' + this.fileUrl
    },

Python

def file_down(request):
    url = request.GET.get('url')
    if url is None:
        return render_json({'result': False, 'data': '请传入文件路径参数url'})
    name_li = url.split('$')
    file_name = name_li[-1]
    f = open(url,'rb')
    response =HttpResponse(f)
    response['Content-Type']='application/octet-stream'  
    response['Content-Disposition']="attachment;filename*=utf-8''{}".format(escape_uri_path(file_name))
    return response

相关文章

网友评论

      本文标题:Element按钮下载文件(后端Python)

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