Vic*_*tor 4 javascript pdf ajax xmlhttprequest axios
为了强制从服务器下载PDF,我尝试使用axios和本机xhr对象.原因是我必须发送post请求,因为我向服务器传递了太多数据,所以带有简单链接的选项(就像site.ru/download-pdf对我来说不起作用).
即使我最终设法用Xhr做到这一点,我仍然不知道为什么axios方法不起作用.
以下是我用xhr执行此操作的方法,它对我有用:
let xhr = new XMLHttpRequest()
xhr.open('POST', Vue.config.baseUrl + `order-results/${id}/export-pdf`, true)
xhr.setRequestHeader("Authorization", 'Bearer ' + this.token())
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xhr.responseType = 'arraybuffer'
xhr.onload = function(e) {
if (this.status === 200) {
let blob = new Blob([this.response], { type:"application/pdf" })
let link = document.createElement('a')
link.href = window.URL.createObjectURL(blob)
link.download = 'Results.pdf'
link.click()
}
};
xhr.send("data=" + data);
Run Code Online (Sandbox Code Playgroud)
这是"axios-way",我实际上得到了具有正确页数的PDF,但它们都是空的:
axios.post(`order-results/${id}/export-pdf`, {
data,
responseType: 'arraybuffer'
}).then((response) => {
let blob = new Blob([response.data], { type:"application/pdf" })
let link = document.createElement('a')
link.href = window.URL.createObjectURL(blob)
link.download = 'Results.pdf'
link.click()
})
Run Code Online (Sandbox Code Playgroud)
Axios已配置为发送授权令牌.我放入Application/x-www-form-urlencodedxhr因为否则我无法在服务器端获取数据.
即使xhr工作,我更喜欢使用axios,因为我到处使用它而且我只是好奇我做错了什么.我尝试了不同的解决方案,只有本地xhr才能完成这项工作.
Nay*_*qui 10
以下适用于我:
axios.post("http://localhost:8080/reports/my-report/",
data,
{
responseType: 'arraybuffer',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/pdf'
}
})
.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();
})
.catch((error) => console.log(error));
Run Code Online (Sandbox Code Playgroud)
如果这有帮助,请告诉我.
干杯!
无论出于何种原因,pdf 正在下载,但传递的任何内容都没有出现,导致 pdf 为空白。
我发现这个代码片段很相似,但会生成包含内容的 pdf 文件。
axios({
url: '/pdf',
method: 'GET',
responseType: 'blob', // important
}).then((response) => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'file.pdf');
document.body.appendChild(link);
link.click();
});
Run Code Online (Sandbox Code Playgroud)
有一些反馈指出它在 IE 11 上不起作用,我还没有测试它,但使用 FileSaver.js 发布了一个解决方案
axios({
url: '/pdf',
method: 'GET',
responseType: 'blob', // important
}).then((response) => {
FileSaver.saveAs(new Blob([response.data]));
});
Run Code Online (Sandbox Code Playgroud)
Ros*_*mon -2
您收到的是空 PDF,因为没有数据传递到服务器。您可以尝试使用这样的数据对象传递数据
axios.post(`order-results/${id}/export-pdf`, {
data: {
firstName: 'Fred'
},
responseType: 'arraybuffer'
}).then((response) => {
console.log(response)
let blob = new Blob([response.data], { type: 'application/pdf' } ),
url = window.URL.createObjectURL(blob)
window.open(url); // Mostly the same, I was just experimenting with different approaches, tried link.click, iframe and other solutions
});
Run Code Online (Sandbox Code Playgroud)
顺便说一句,我非常感谢您向我展示了从回复中下载 pdf 的提示。谢谢你:)
var dates = {
fromDate: 20/5/2017,
toDate: 25/5/2017
}
Run Code Online (Sandbox Code Playgroud)
我使用的方式是,
axios({
method:'post',
url:'/reports/interval-dates',
responseType:'arraybuffer',
data: dates
})
.then(function(response) {
let blob = new Blob([response.data], { type: 'application/pdf' } );
let link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = 'Report.pdf';
link.click();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7371 次 |
| 最近记录: |