1、需要使用的方法:
wx.request(); //發(fā)請求調(diào)用接口
wx.showModal();//打開彈框進(jìn)行展示
wx.setClipboardData();//復(fù)制文本的數(shù)據(jù)
wx.downloadFile();//文件下載的方法
wx.getFileSystemManager().saveFile();//保存數(shù)據(jù)
wx.saveImageToPhotosAlbum();//保存數(shù)據(jù)到相冊
2、在微信小程序中,用戶使用的手機分蘋果手機和安卓手機;安卓手機保存文件我們是可以找到在手機磁盤中找到文件的,蘋果手機是找不到指定文件的;那么蘋果手機則是返回文件的路徑提供可復(fù)制功能在瀏覽器進(jìn)行打開下載;
3、另外目前在保存zip、word、excel目前采用的是直接已圖片的形式保存在手機相冊中,然后通過修改文件的后綴來實現(xiàn)的(圖片資源可以直接導(dǎo)出到手機相冊);
4、多的話就不說了,直接上在實際開發(fā)中的源碼:
// 導(dǎo)出zip數(shù)據(jù)
exportProject(){
var strCode = ''
var arr = JSON.parse(JSON.stringify(this.data.dataList))
arr.length>0 && arr.forEach((item,index) =>{
if(item.checked) {
strCode += `${item.id},`
}
})
// 有勾選導(dǎo)出勾選 沒有勾選導(dǎo)出所有的數(shù)據(jù)
if(strCode) {
strCode = strCode.substr(0,strCode.length-1)
} else {
wx.showToast({
title: '請先勾選在進(jìn)行導(dǎo)出!',
icon: 'none',
duration: 2000
})
return
}
wx.showLoading({
title: '保存中...'
})
wx.request({
url: `http://localhost:6666/upload/upload`, //下載文件的接口
method: 'GET',
header: {
'Content-Type': 'application/json', // 默認(rèn)值
'accessToken': accessToken
},
success(resUrl){
//首先判斷手機的類型是ios還是Android 判斷的方法微信小程序有api進(jìn)行調(diào)用
if(wx.getStorageSync('flagIosOrAndroid') == 'ios') { //蘋果手機直接彈出地址進(jìn)行復(fù)制操作
wx.hideLoading()
wx.showModal({
title: '下載地址',
content: resUrl,
showCancel: false,
confirmText: '復(fù)制' ,
success (res) {
if (res.confirm) {
wx.setClipboardData({//復(fù)制文本
data: resUrl.data,
success: function (res) {
wx.showToast({
title: '復(fù)制成功',
icon:"none",
mask:"true"//是否設(shè)置點擊蒙版,防止點擊穿透
})
}
})
}
}
})
} else { //安卓手機將文件保存為png 導(dǎo)出之后改后綴
wx.downloadFile({
url: `${resUrl}`, //僅為示例,并非真實的資源
success (resCode) {
wx.hideLoading()
var savePath = wx.env.USER_DATA_PATH + `/Zip.png`;
wx.getFileSystemManager().saveFile({
tempFilePath: resCode.tempFilePath||resCode.filePath,
filePath:savePath,
success: function (res){
//保存圖片
wx.saveImageToPhotosAlbum({
filePath:res.savedFilePath,
success(res) {
//這里可以出個彈窗,給用戶提醒,讓他去文件管理器里面找到后重命名
wx.showModal({
title: '文件保存成功',
content: `請在文件管理器找到Zip.png修改后綴為zip`,
showCancel: false,
confirmText: '確定' ,
success (res) {
}
})
}
})
},
})
},
fail(err){
wx.hideLoading()
wx.showToast({
title: '文件下載失敗!',
icon: 'none',
duration: 2000
})
}
})
}
},
fail(err) {
wx.hideLoading()
wx.showToast({
title: '文件獲取失敗!',
icon: 'none',
duration: 2000
})
}
})
},
總結(jié):目前采用的是這種方法來進(jìn)行導(dǎo)出到手機的 ,示例為導(dǎo)出zip的例子;導(dǎo)出excel和word方法都類似,導(dǎo)出圖片的也是類似;有其他的好的方法也可以給我推薦推薦,我們一起學(xué)習(xí),互相交流。覺得有用記得點贊加關(guān)注哦,方便下次用的時候不迷路。
文章來源地址http://www.zghlxwxcb.cn/news/detail-521400.html
文章來源:http://www.zghlxwxcb.cn/news/detail-521400.html
到了這里,關(guān)于微信小程序?qū)С鰖ip、excel、word、png到手機的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!