下載EXCEL vue模板

在vue框架中:
https://mp.weixin.qq.com/s/Rdc-PcQCQstgC1QubwTO_Q
在mako框架中只能先將excel放在一個固定的文件夾下,例如:upload目錄下,然后獲取到其絕對路徑傳到前端即可。

<%inherit file="/base.html"/>

<%block name="content">
<div id="app">
    <a :href="site_url + 'download_word_?url=' + file_path_">點擊下載</a>
</div>
</%block>
<script>
    Vue.prototype.$http = axios;
    Vue.use(iview);
    new Vue({
        el: '#app',
        data() {
            return {
                site_url: window.site_url,
                file_path_: '',
            }
        },
        methods: {
            get_path() {
                this.$http.get('${SITE_URL}get_path/').then(res => {
                    if (res.data.result) {
                        this.file_path_ = res.data.path
                    } else {
                        this.$Message.error(res.data.message)
                    }
                })
            }
        },
        mounted() {
            this.get_path()
        }
    })
</script>
<style></style>

特別注意的是不能用\斜杠,否則會報錯,對應(yīng)后端代碼為:

def get_path(request):
    try:
        path = PROJECT_ROOT + '/upload/new.xlsx'
    except Exception as e:
        return render_json({'result': False, 'message': "獲取路徑失敗"})
    return render_json({'result': True, 'path': path})
def download_word_(request):
    # 獲取前端傳遞過來的保存文件的url
    url = request.GET.get('url')
    if url is None:
        return render_json({'result': False, 'data': '請傳入文件路徑參數(shù)url'})
    name_li = url.split('/')
    file_name = name_li[-1]
    # 獲取文件名
    file_name = file_name.split('/')[-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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容