ruby-on-rails - 文件在 rails 中始终以 PDF 格式下载
问题描述
我在我的 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>
解决方案
我遇到了类似的问题,以下对我有用。
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"}
控制台中。您不必指定类型。
推荐阅读
- python - 为什么 np.hypot 和 np.subtract.outer 与香草广播相比非常快?使用 Numba 并行加速 numpy 进行距离矩阵计算
- web-services - API 网关可以指向 Web 服务 (SOAP),还是可以在微服务中使用 SOAP,以便 API 网关指向它?
- python - 如何修复 .repo/repo/main.py 无效语法问题(Ubuntu 20.04)
- python - 在线镜像
- spring - Querydsl 是否可以防止 SQL 注入
- javascript - 返回不等待if条件
- php - Laravel Nginx PHP Fastcgi 怎么找?
- angular - 在一个页面中使用多个选项卡控件每个选项卡控件都有一个组件
- paypal - 某些帐户的 PayPal 付款失败
- oracle - 执行 OracleDataAdapter.Fill 时程序没有响应