微信小程序的文件預覽須要先使用
wx.downloadFile
下載文件,而後使用下載文件的臨時路徑經過wx.openDocument
進行文件的預覽javascript項目中,有的須要打開pdf 來閱讀信息。就須要用小程序自帶的api方法。java
<button bindtap='preview'>工做簡歷預覽</button>
//簡歷預覽 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) } }) } }) } },