Element按鈕下載文件(後端Python)

HTMLjson

<el-button class="btns" size='mini' type="success" @click='downloadFun'>下載</el-button>
複製代碼

JSbash

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

Pythonapp

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
複製代碼
相關文章
相關標籤/搜索