好吧,我做错了。我的解决方案是:
// @param url : the url of the file on server
function downloadHTML (url) {
url = new URL(url);
return axios({
// I've created a GET API route on my server using the pathname of the file url, which returns the content of the file
url: url.origin + '/api' + url.pathname,
method: 'GET',
responseType: 'blob',
}).then((response) => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'file.pdf'); //or any other extension
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
});
}