Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Vue+;拉威尔:如何正确下载PDF文件?_Javascript_Php_Laravel_Pdf_Vue.js - Fatal编程技术网

Javascript Vue+;拉威尔:如何正确下载PDF文件?

Javascript Vue+;拉威尔:如何正确下载PDF文件?,javascript,php,laravel,pdf,vue.js,Javascript,Php,Laravel,Pdf,Vue.js,情况: $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(respo

情况:

$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()
    })
}
前端:Vue。后端:拉威尔

在web应用程序中,我需要让用户下载某些pdf文件:

  • 我需要Laravel获取该文件并将其作为API GET请求的响应返回
  • 然后在我的Vue web应用程序中,我需要获取文件并下载它
代码:

$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()
    })
}
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()
    })
}
网络应用程序:

$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()
    })
}
结果:

$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文件-相同的结果)

来自服务器的响应:

$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:

$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文件上。它看起来确实是损坏的数据。这是它看起来如何像
响应的摘录。数据

问题:

$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()
    })
}
如何使用Laravel为API和Vue为web应用正确下载pdf文件


谢谢

您将无法从
Laravel
下载到
Vue
,因为我假设这两个版本都在不同的端口上运行

即使你尝试这样的事情

public function getDownload()
    {
        //PDF file is stored under project/public/download/info.pdf
        $file= public_path(). "/download/info.pdf";

        $headers = [
              'Content-Type' => 'application/pdf',
           ];

    return response()->download($file, 'filename.pdf', $headers);
    }
如果要将标题发送到
Laravel
端口,请尝试使用
Vue js
库并尝试在库中发送该pdf内容,这将不会有任何帮助

试试这个
解决方案:

$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()
    })
}
上面的代码是正确的。缺少的是将适当的
响应类型添加为
arraybuffer

我被回复中的
吓到了,这误导了我。 这些问号还可以,因为pdf是二进制数据,应该由合适的阅读器读取

阵列缓冲区:

$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()
    })
}
而arraybuffer正是用来保存二进制数据的

这是mozilla网站上的定义:

ArrayBuffer对象用于表示通用的固定长度 原始二进制数据缓冲区。您不能直接操作的内容 一个排列的缓冲器;而是创建一个类型化数组对象或 以特定格式表示缓冲区的DataView对象, 并使用它读取和写入缓冲区的内容

ResponseType
字符串指示响应的类型。通过告诉它一个arraybuffer,它会相应地处理数据

通过添加responseType,我成功地下载了pdf文件

代码:

$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()
    })
}
这是更正的Vue代码(与之前完全相同,但添加了responseType):

编辑:

$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()
    })
}
这是一个考虑到其他浏览器行为的更完整的解决方案:

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)
},
这对我很有用

从laravel后端:

$pdf = PDF::loadView('your_view_name', ['data' => $data]);
return $pdf->output();
从vuejs前端:

axios({
url: 'http://localhost:8000/api/your-route',
method: 'GET',
responseType: 'blob',
}).then((response) => {
     var fileURL = window.URL.createObjectURL(new Blob([response.data]));
     var fileLink = document.createElement('a');
     fileLink.href = fileURL;
     fileLink.setAttribute('download', 'file.pdf');
     document.body.appendChild(fileLink);
     fileLink.click();

});

看起来很相似。我将尝试使用Axios而不是vue-resource。请务必让我知道它是否有效@JohnnyFittizio在您提供的链接中,我找到了实际的解决方案。除了缺少响应类型之外,一切都是正确的。Axios或vue资源并不重要。我在答案中写下了正确的代码。我相信你指出了正确的方向。谢谢在您的答案中添加一些讨论,以帮助解释它如何解决针对Vue 2.5.22和Vue resource 1.5.1的OP问题。非常感谢@FrancescoMussi!这很有帮助。我的pdf下载但无法打开,或打开时出现无法打开的错误。当我添加responseType时,一切正常!响应类型为arraybuffer!工作我已经在流式PDF数据上工作了大约两天了,现在我正在努力工作。我一直把“未定义”作为我的PDF数据。我可以在网络中看到该死的PDF数据,但不知怎的,我无法保存它!