美文网首页
Vue axios Codeigniter下载文件(浏览器不预览

Vue axios Codeigniter下载文件(浏览器不预览

作者: BlueFishMan | 来源:发表于2020-05-21 13:45 被阅读0次

前端请求

this.axios.get('***/download', {
                    params: {
                        id: ***
                    },
                    responseType: "blob"
                }).then(function (response) {
                    if (response.status === 200) {
                        var url = window.URL.createObjectURL(response.data);
                        var a = document.createElement("a");
                        document.body.appendChild(a);
                        a.href = url;
                        a.download = 'output.txt';
                        a.click();
                    }
                }).catch(function (error) {
                    console.log(error);
                });

后端响应

public function download()
    {
        if ($this->input->method(TRUE) == 'GET') {
            $id = $this->input->get('id', TRUE);

            $this->output
                ->set_status_header(200)
                ->set_content_type('blob')
                ->set_output(file_get_contents(base_url("/assets/" . $id)));
        } elseif ($this->input->method(TRUE) == 'OPTIONS') {
            $this->output
                ->set_status_header(200)
                ->set_content_type('application/json');
        } else {
            $this->output
                ->set_status_header(405)
                ->set_content_type('application/json');
        }
    }

相关文章

网友评论

      本文标题:Vue axios Codeigniter下载文件(浏览器不预览

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