1.判断附件类型进行具体操作
//查看附件材料详情
lookFjcl(item) {
let url =`${location.origin}${API_BASE_URL}/upload/file-getFile?fileId=${item.fileId}&accessToken=${res.data.data}&fileName=${item.fileName}`
// console.log('当前附件地址', url);
if (url.endsWith(".pdf") || url.endsWith(".PDF")) {
//转码下载的链接
url = encodeURIComponent(url);
this.pdfUrl =`${location.origin}${API_BASE_URL}/static/lib/pdfjs/web/viewer.html?file=${url}`;
this.pdfShow = true;
} else if (url.endsWith('.TXT') || url.endsWith('.txt')) {
let xhr = new XMLHttpRequest()
xhr.open("GET", url, false); // 线上链接地址
xhr.overrideMimeType("text/html;charset=gbk")
xhr.send(null);
// console.log(xhr.responseText) // xhr.responseText为文本中的内容
this.txtStr = xhr.responseText
this.txtShow = true
} else if (url.endsWith(".docx") || url.endsWith(".doc") || url.endsWith(".DOCX") || url
.endsWith(".DOC")) {
//#ifdef APP-PLUS
this.downloadFile(url)
//#endif
//#ifdef H5
//非同源图片将直接打开
let abtn = document.createElement('a');
abtn.href = url;
abtn.download = item.fileName;
abtn.target = '_blank';
abtn.style = 'display: none'
abtn.click();
//#endif
} else if (url.endsWith(".png") || url.endsWith(".jpg") || url.endsWith(".jpeg")) {
this.imageUrls = []
let img = {
'url1': url
}
this.imageUrls.push(img);
// console.log('imageUrls', this.imageUrls)
this.imgShow = true
}
},
//下载文件
downloadFile(src) {
uni.downloadFile({
url: src, // 要下载的文件的 url
success: function(res) {
if (res.statusCode === 200) {
// 下载成功,将文件保存到本地
uni.saveFile({
tempFilePath: res.tempFilePath,
success: function() {
// console.log('保存成功')
},
fail: function(err) {
// console.log('保存失败:', err)
}
})
}
},
fail: function(err) {
console.log('下载失败:', err)
}
})
},
网友评论