微信小程序如何實現文件預覽?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
微信小程序的文件預覽需要先使用wx.downloadFile下載文件,然后使用下載文件的臨時路徑通過wx.openDocument進行文件的
預覽
wxml代碼:
<button bindtap='preview'>簡歷預覽</button>
js代碼:
//簡歷預覽
preview: function () {
var that = this;
console.log("簡歷預覽")
//這里的value是先在data里面初始化,然后我根據用戶切換單選框,獲取的簡歷文件的主鍵id
console.log(this.data.value)
var id = that.data.value;
if (id == "") {
wx.showModal({
title: '',
content: '請選擇一份簡歷',
showCancel: false,
confirmColor: "#FFB100"
})
} else {
//先通過簡歷的主鍵id,查詢簡歷路徑(大家可以根據自己的需求來傳數據)
wx.request({
url: app.globalData.url + "/api/interview/queryFilePath",
data: {
id: id
},
method: 'POST',
header: { "content-type": "application/x-www-form-urlencoded" },
success: function (res) {
console.log(res.data)
that.setData({
path: res.data.path,
type: res.data.type
})
//下載簡歷
wx.downloadFile({
url: app.globalData.url + that.data.path,
success: function (res) {
var filePath = res.tempFilePath
console.log(filePath)
//預覽簡歷
wx.openDocument({
filePath: filePath,
fileType: that.data.type,
success: function (res) {
console.log("打開文檔成功")
console.log(res);
},
fail: function (res) {
console.log("fail");
console.log(res)
},
complete: function (res) {
console.log("complete");
console.log(res)
}
})
},
fail: function (res) {
console.log('fail')
console.log(res)
},
complete: function (res) {
console.log('complete')
console.log(res)
}
})
}
})
}
},關于微信小程序如何實現文件預覽問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。