微信小程序下载文件ios无法预览问题–解决方案
想给单位做一个微信小程序,实现文件上传下载的功能,方便办事,也能提高办事效率。
wx.downloadFile()获取到的文件缓存在手机上,iOS无法在手机预览这个缓存文件,安卓手机可以。
这个需要在调用downloadFile指定保存文件路径,才可以预览操作文件,在临时路径是无法转发编辑的。
var fileTypes = ["DOC", "XLS", "PPT", "PDF", "DOCX", "XLSX", "PPTX"];
var imageTypes = ["JPG", "JPEG", "PNG"];
var FilePath = wx.env.USER_DATA_PATH + "/"+ fileId + "."+fileType;
wx.downloadFile({
url: app.globalData.apiUrl + "certapps/attachment?id=" + fileId,
filePath: FilePath,
method: "GET",
complete: function(res) {
wx.hideLoading();
},
header: {
'Content-Type': 'application/json',
'token': token
},
success: function(res) {
wx.hideLoading();
if (res.statusCode === 200) {
if (fileTypes.indexOf(fileType) != -1) {
wx.openDocument({
filePath: res.filePath,
success: function(res) {
console.log('打开文档成功')
},
fail: function(res) {
console.log(res)
wx.hideLoading();
wx.showModal({
title: '提示',
content: "下载失败!",
showCancel: false
})
}
})
} else if (imageTypes.indexOf(fileType) != -1) {
wx.previewImage({
current: res.filePath, // 当前显示图片的http链接
urls: [res.filePath] // 需要预览的图片http链接列表
})
} else {
wx.showModal({
title: '提示',
content: "文件类型不支持预览",
showCancel: false
})
}
} else if (res.statusCode !== 200) {
wx.showModal({
title: '提示',
content: "下载失败!",
showCancel: false
})
}
},
fail: function(res) {
wx.hideLoading();
wx.showModal({
title: '提示',
content: "下载失败!",
showCancel: false
})
}
})