【问题标题】:File always downloaded as PDF in rails文件在 rails 中始终以 PDF 格式下载
【发布时间】:2020-07-27 20:03:06
【问题描述】:

我在我的 rails 应用程序中定义了一个上传器。我在这里面临的问题是,当我下载上传的文件时,它会下载为 PDF 而不是文件的实际格式。

class DocumentController < MyAccountController
  def show
   @agency = current_agency
   redirect_to @agency.document.url
  end
end

这是文件的网址。但文件总是以 PDF 格式下载

"http://localhost:3000/uploads/agency/document/61/document.jpeg"

文件下载方法

<script>
 downloadDocument() {
   var url = 'myagency/document',
   fileName = "EngagementLetter",
   file;
   this.$axios.get(url, { responseType: 'blob' })
   .then(response => {
    file = new Blob(
    [response.data],
    { type: 'application/pdf, image/gif, image/jpeg' }
   );
   FileSaver.saveAs(file, fileName);
   });
  }
</script>

【问题讨论】:

    标签: ruby-on-rails vue.js carrierwave


    【解决方案1】:

    我遇到了类似的问题,以下对我有用。

          var url = 'myagency/document',
          fileName = "EngagementLetter",
    
          axios.get(url, { responseType: 'blob' })
          .then(response => {
            console.log(response.data)
            const url = window.URL.createObjectURL(response.data)
    
            const link = document.createElement('a')
            link.href = url
            link.setAttribute('download', fileName )
            document.body.appendChild(link)
            link.click()
            link.remove()
          });
    

    您会注意到response.data 在控制台中是Blob {size: ####, type: "image/jpeg"}。您不必指定类型。

    【讨论】:

      猜你喜欢
      • 2021-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多