欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序預(yù)覽二進(jìn)制流文件的方法

 更新時(shí)間:2022年08月28日 11:57:53   作者:花辭榭柳  
這篇文章主要為大家詳細(xì)介紹了微信小程序預(yù)覽二進(jìn)制流文件的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

微信小程序?qū)⒑蠖私涌诜祷氐亩M(jìn)制流PDF 文件在線(xiàn)預(yù)覽,供大家參考,具體內(nèi)容如下

一、微信小程序的文件系統(tǒng)

微信小程序文件系統(tǒng)參考官方文檔:微信小程序文檔
我們主要是把后端接口獲取到的pdf二進(jìn)制流,下載保存到微信的本地用戶(hù)文件,下載后預(yù)覽再刪掉,因?yàn)楸镜赜脩?hù)文件每個(gè)用戶(hù)只有200M,所以預(yù)覽后刪掉。

二、小程序?qū)崿F(xiàn)文件預(yù)覽

代碼如下(示例):

//使用登記牌掃碼查詢(xún)
usequercode() {
?? ?uni.scanCode({
?? ??? ??? onlyFromCamera: true,
?? ??? ??? ??? ?success: function(res) {
?? ??? ??? ??? ??? ?wx.showLoading({
?? ??? ??? ??? ??? ??? ?title: '正在識(shí)別中....'
?? ??? ??? ??? ??? ?});
?? ??? ??? ??? ??? ?//掃描二維碼
?? ??? ??? ??? ??? ?if (res.scanType != 'QR_CODE') {
?? ??? ??? ??? ??? ??? ?uni.showToast({
?? ??? ??? ??? ??? ??? ??? ?title: '請(qǐng)掃描正確的使用登記牌二維碼',
?? ??? ??? ??? ??? ??? ??? ?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('條碼類(lèi)型:' + res.scanType);
?? ??? ??? ??? ??? ?console.log('條碼內(nèi)容:' + res.result);
?? ??? ??? ??? ??? ?//再文件下載后的用處
?? ??? ??? ??? ??? ?const result = res.result;
?? ??? ??? ??? ??? ?const applyid = result.substring(result.lastIndexOf('=') + 1); //獲取到條碼內(nèi)容,獲取參數(shù)訪問(wèn)接口
?? ??? ??? ??? ??? ?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', //此處是請(qǐng)求文件流,必須帶入的屬性
?? ??? ??? ??? ??? ??? ?success: res => {
?? ??? ??? ??? ??? ??? ??? ?console.log(res);
?? ??? ??? ??? ??? ??? ??? ?var data = res.data;
?? ??? ??? ??? ??? ??? ??? ?//將接口返回的二進(jìn)制數(shù)據(jù)轉(zhuǎn)成pdf文件
?? ??? ??? ??? ??? ??? ??? ?//uni.getFileSystemManager()
?? ??? ??? ??? ??? ??? ??? ?wx.getFileSystemManager().writeFile({
?? ??? ??? ??? ??? ??? ??? ??? ?// 寫(xiě)文件
?? ??? ??? ??? ??? ??? ??? ??? ?filePath: wx.env.USER_DATA_PATH + '/使用登記牌.pdf', // wx.env.USER_DATA_PATH 指定臨時(shí)文件存入的路徑,后面字符串自定義
?? ??? ??? ??? ??? ??? ??? ??? ?data: data,
?? ??? ??? ??? ??? ??? ??? ??? ?encoding: 'binary', //二進(jìn)制流文件必須是 binary
?? ??? ??? ??? ??? ??? ??? ??? ?success(res) {
?? ??? ??? ??? ??? ??? ??? ??? ??? ?wx.openDocument({
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?// 新開(kāi)頁(yè)面打開(kāi)文檔
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?filePath: wx.env.USER_DATA_PATH + '/使用登記牌.pdf', //拿上面存入的文件路徑
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?success: function(res) {
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?console.log(res);
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?setTimeout(() => {
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?wx.hideLoading();
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?}, 500);
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ???//查看后刪除本地用戶(hù)文件
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ???//wx.getFileSystemManager().unlink({
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ???// 寫(xiě)文件
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ???//filePath: wx.env.USER_DATA_PATH + '/使用登記牌.pdf', // wx.env.USER_DATA_PATH 指定臨時(shí)文件存入的路徑,后面字符串自定義

?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??//success(res) {
?? ??? ??? ??? ??? ??? ??? ??? ??? ???  ?//console.log(res);
?? ??? ??? ??? ??? ??? ??? ??? ???//}
?? ??? ??? ??? ??? ??? ??? ?? ?//});
?? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ??? ??});
?? ??? ??? ??? ??? ??? ?? ?},
?? ??? ??? ??? ??? ??error(error) {
?? ??? ??? ??? ?console.log(error);
?? ??? ??? ??? ??? ? ?}
?? ??? ??? ??? ????});
?? ??? ??? ??? ?}
?? ??? ???  ?});
?? ??? ???}
?? ??});
},

三、實(shí)現(xiàn)效果圖

總結(jié)

我本來(lái)想打開(kāi)后刪除的,再真機(jī)調(diào)試下沒(méi)問(wèn)題,但發(fā)布在手機(jī)上提示預(yù)覽失敗,請(qǐng)?jiān)谄渌麘?yīng)用打開(kāi),刪除寫(xiě)在compltet也一樣,后面考慮到服務(wù)器資源內(nèi)存資源比存儲(chǔ)性能高,由放在服務(wù)器上了pdf。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論