uniapp 根據(jù)附件類型查看或下載附件

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('當(dāng)前附件地址', url);
                        if (url.endsWith(".pdf") || url.endsWith(".PDF")) {
                            //轉(zhuǎn)碼下載的鏈接
                            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為文本中的內(nèi)容
                            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)
                    }
                })
            },
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容