微信小程序預覽二進制流文件的方法
微信小程序?qū)⒑蠖私涌诜祷氐亩M制流PDF 文件在線預覽,供大家參考,具體內(nèi)容如下
一、微信小程序的文件系統(tǒng)
微信小程序文件系統(tǒng)參考官方文檔:微信小程序文檔
我們主要是把后端接口獲取到的pdf二進制流,下載保存到微信的本地用戶文件,下載后預覽再刪掉,因為本地用戶文件每個用戶只有200M,所以預覽后刪掉。
二、小程序?qū)崿F(xiàn)文件預覽
代碼如下(示例):
//使用登記牌掃碼查詢
usequercode() {
?? ?uni.scanCode({
?? ??? ??? onlyFromCamera: true,
?? ??? ??? ??? ?success: function(res) {
?? ??? ??? ??? ??? ?wx.showLoading({
?? ??? ??? ??? ??? ??? ?title: '正在識別中....'
?? ??? ??? ??? ??? ?});
?? ??? ??? ??? ??? ?//掃描二維碼
?? ??? ??? ??? ??? ?if (res.scanType != 'QR_CODE') {
?? ??? ??? ??? ??? ??? ?uni.showToast({
?? ??? ??? ??? ??? ??? ??? ?title: '請掃描正確的使用登記牌二維碼',
?? ??? ??? ??? ??? ??? ??? ?duration: 2000,
?? ??? ??? ??? ??? ??? ??? ?icon: 'none'
?? ??? ??? ??? ??? ??? ?});
?? ??? ??? ??? ??? ??? ?return;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?//未攜帶需要的參數(shù)
?? ??? ??? ??? ??? ?else if (res.result.indexOf('applyId') < 0 || res.result.indexOf('applyType') < 0) {
?? ??? ??? ??? ??? ??? ?uni.showToast({
?? ??? ??? ??? ??? ??? ??? ?title: '解析二維碼中存在非法參數(shù)',
?? ??? ??? ??? ??? ??? ??? ?duration: 2000,
?? ??? ??? ??? ??? ??? ??? ?icon: 'none'
?? ??? ??? ??? ??? ??? ?});
?? ??? ??? ??? ??? ??? ?return;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?console.log('條碼類型:' + res.scanType);
?? ??? ??? ??? ??? ?console.log('條碼內(nèi)容:' + res.result);
?? ??? ??? ??? ??? ?//再文件下載后的用處
?? ??? ??? ??? ??? ?const result = res.result;
?? ??? ??? ??? ??? ?const applyid = result.substring(result.lastIndexOf('=') + 1); //獲取到條碼內(nèi)容,獲取參數(shù)訪問接口
?? ??? ??? ??? ??? ?const applytype = result.substring(result.indexOf('=') + 1, result.indexOf('&'));
?? ??? ??? ??? ??? ?console.log(applyid);
?? ??? ??? ??? ??? ?console.log(applytype);
?? ??? ??? ??? ??? ?//讀取本地文件
?? ??? ??? ??? ??? ?const fs = uni.getFileSystemManager();
?? ??? ??? ??? ??? ?uni.request({
?? ??? ??? ??? ??? ??? ?url: getApp().globalData.config.url + 'enterprise/useApply/usingCodeUpload',
?? ??? ??? ??? ??? ??? ?method: 'POST',
?? ??? ??? ??? ??? ??? ?headers: { isToken: false },
?? ??? ??? ??? ??? ??? ?data: { applyId: applyid, applyType: applytype },
?? ??? ??? ??? ??? ??? ?responseType: 'arraybuffer', //此處是請求文件流,必須帶入的屬性
?? ??? ??? ??? ??? ??? ?success: res => {
?? ??? ??? ??? ??? ??? ??? ?console.log(res);
?? ??? ??? ??? ??? ??? ??? ?var data = res.data;
?? ??? ??? ??? ??? ??? ??? ?//將接口返回的二進制數(shù)據(jù)轉(zhuǎn)成pdf文件
?? ??? ??? ??? ??? ??? ??? ?//uni.getFileSystemManager()
?? ??? ??? ??? ??? ??? ??? ?wx.getFileSystemManager().writeFile({
?? ??? ??? ??? ??? ??? ??? ??? ?// 寫文件
?? ??? ??? ??? ??? ??? ??? ??? ?filePath: wx.env.USER_DATA_PATH + '/使用登記牌.pdf', // wx.env.USER_DATA_PATH 指定臨時文件存入的路徑,后面字符串自定義
?? ??? ??? ??? ??? ??? ??? ??? ?data: data,
?? ??? ??? ??? ??? ??? ??? ??? ?encoding: 'binary', //二進制流文件必須是 binary
?? ??? ??? ??? ??? ??? ??? ??? ?success(res) {
?? ??? ??? ??? ??? ??? ??? ??? ??? ?wx.openDocument({
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?// 新開頁面打開文檔
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?filePath: wx.env.USER_DATA_PATH + '/使用登記牌.pdf', //拿上面存入的文件路徑
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?success: function(res) {
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?console.log(res);
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?setTimeout(() => {
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?wx.hideLoading();
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?}, 500);
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ???//查看后刪除本地用戶文件
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ???//wx.getFileSystemManager().unlink({
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ???// 寫文件
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ???//filePath: wx.env.USER_DATA_PATH + '/使用登記牌.pdf', // wx.env.USER_DATA_PATH 指定臨時文件存入的路徑,后面字符串自定義
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??//success(res) {
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?//console.log(res);
?? ??? ??? ??? ??? ??? ??? ??? ???//}
?? ??? ??? ??? ??? ??? ??? ?? ?//});
?? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ??? ??});
?? ??? ??? ??? ??? ??? ?? ?},
?? ??? ??? ??? ??? ??error(error) {
?? ??? ??? ??? ?console.log(error);
?? ??? ??? ??? ??? ? ?}
?? ??? ??? ??? ????});
?? ??? ??? ??? ?}
?? ??? ??? ?});
?? ??? ???}
?? ??});
},三、實現(xiàn)效果圖

總結
我本來想打開后刪除的,再真機調(diào)試下沒問題,但發(fā)布在手機上提示預覽失敗,請在其他應用打開,刪除寫在compltet也一樣,后面考慮到服務器資源內(nèi)存資源比存儲性能高,由放在服務器上了pdf。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
js中使用DOM復制(克?。┲付ü?jié)點名數(shù)據(jù)到新的XML文件中的代碼
使用DOM復制(克隆)指定節(jié)點名數(shù)據(jù)到新的XML文件中 ,用到三個類的相關知識點 : DOMDocument - DOMNodeList - DOMNode2011-07-07

