前端请求
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');
}
}
网友评论