Ruby on rails 文件始终以PDF格式在rails中下载
我在rails应用程序中定义了一个上传程序。我在这里面临的问题是,当我下载上传的文件时,它是以PDF格式下载的,而不是文件的实际格式Ruby on rails 文件始终以PDF格式在rails中下载,ruby-on-rails,vue.js,carrierwave,Ruby On Rails,Vue.js,Carrierwave,我在rails应用程序中定义了一个上传程序。我在这里面临的问题是,当我下载上传的文件时,它是以PDF格式下载的,而不是文件的实际格式 class DocumentController < MyAccountController def show @agency = current_agency redirect_to @agency.document.url end end <script> downloadDocument() { var url
class DocumentController < MyAccountController
def show
@agency = current_agency
redirect_to @agency.document.url
end
end
<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>
方法下载该文件
class DocumentController < MyAccountController
def show
@agency = current_agency
redirect_to @agency.document.url
end
end
<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>
下载文件(){
变量url='myagency/document',
fileName=“约定函”,
文件
这个.axios.get(url,{responseType:'blob'})
。然后(响应=>{
file=newblob(
[答复.数据],
{键入:'application/pdf、image/gif、image/jpeg'}
);
FileSaver.saveAs(文件,文件名);
});
}
我也遇到过类似的问题,以下几点对我有效
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:#####,在控制台中键入:“image/jpeg”}
。您不必指定类型