javascript – Vue Laravel:如何正确下载PDF文件?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – Vue Laravel:如何正确下载PDF文件?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3224字,纯文字阅读大概需要5分钟。
内容图文
情况:
前端:Vue.后端:Laravel.
在Web应用程序内部,我需要让用户下载某些pdf文件:
>我需要Laravel获取文件并将其作为API GET请求的响应返回.
>然后在我的Vue Web应用程序中,我需要获取文件并下载它.
代码:
API:
$file = public_path() . "/path/test.pdf";
$headers = [
'Content-Type' => 'application/pdf',
];
return response()->download($file, 'test.pdf', $headers);
网络应用:
downloadFile() {
this.$http.get(this.apiPath + '/download_pdf')
.then(response => {
let blob = new Blob([response.data], { type: 'application/pdf' })
let link = document.createElement('a')
link.href = window.URL.createObjectURL(blob)
link.download = 'test.pdf'
link.click()
})
}
结果:
使用此代码,我设法下载pdf文件.问题是pdf是空白的.
不知怎的,数据被破坏了(不是这个特殊的pdf文件的问题,我尝试了几个pdf文件 – 相同的结果)
来自服务器的响应:
来自服务器的响应本身很好:
PDF:
问题可能出在pdf文件中.它肯定看起来已损坏的数据.这是它看起来像response.data的摘录:
问题:
如何使用Laravel为Web应用程序正确下载pdf文件和Vue?
谢谢!
解决方法:
解:
适当的归功于@Sagar指出我正确的方向.
上面的代码是正确的.缺少的是将正确的responseType添加为arraybuffer.
我吓到了那些????在回应中,这误导了我.
这些问号是正常的,因为pdf是一个二进制数据,并且应由适当的读者阅读.
ARRAYBUFFER:
而arraybuffer正好用于保存二进制数据.
这是mozilla网站的定义:
The ArrayBuffer object is used to represent a generic, fixed-length
raw binary data buffer. You cannot directly manipulate the contents of
an ArrayBuffer; instead, you create one of the typed array objects or
a DataView object which represents the buffer in a specific format,
and use that to read and write the contents of the buffer.
ResponseType字符串表示响应的类型.通过告诉它的arraybuffer,它会相应地处理数据.
只需添加responseType,我就可以正确下载pdf文件.
代码:
这是更正的Vue代码(与之前完全相同,但添加了responseType):
downloadFile() {
this.$http.get(this.appApiPath + '/testpdf', {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 = 'test.pdf'
link.click()
})
}
编辑:
这是一个更完整的解决方案,考虑到其他浏览器的行为:
downloadContract(booking) {
this.$http.get(this.appApiPath + '/download_contract/' + booking.id, {responseType: 'arraybuffer'})
.then(response => {
this.downloadFile(response, 'customFilename')
}, response => {
console.warn('error from download_contract')
console.log(response)
// Manage errors
}
})
},
downloadFile(response, filename) {
// It is necessary to create a new blob object with mime-type explicitly set
// otherwise only Chrome works like it should
var newBlob = new Blob([response.body], {type: 'application/pdf'})
// IE doesn't allow using a blob object directly as link href
// instead it is necessary to use msSaveOrOpenBlob
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(newBlob)
return
}
// For other browsers:
// Create a link pointing to the ObjectURL containing the blob.
const data = window.URL.createObjectURL(newBlob)
var link = document.createElement('a')
link.href = data
link.download = filename + '.pdf'
link.click()
setTimeout(function () {
// For Firefox it is necessary to delay revoking the ObjectURL
window.URL.revokeObjectURL(data)
}, 100)
},
内容总结
以上是互联网集市为您收集整理的javascript – Vue Laravel:如何正确下载PDF文件?全部内容,希望文章能够帮你解决javascript – Vue Laravel:如何正确下载PDF文件?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。