//文件预览
function readfile(url, name, type, cb) {
//url-文件地址
//name-文件名
//type-文件类型
//cb-回调函数
if (type == "jpg" || type == "png" || type == "jpeg") {
var list = [url]
console.log(url);
wx.previewImage({
current: url, // 当前显示图片的http链接
urls: list // 需要预览的图片http链接列表
})
return
} else if (type == 'mp4') {
//视频预览自定义视频播放页面
wx.navigateTo({
url: '/pages/filesopen/filesopen?url=' + url,
})
} else if (type == 'mp3') {
wx.navigateTo({
url: '/pages/audioplay/audioplay?url=' + url + '&name=' + name,
})
} else {
wx.showLoading({
title: '加载中'
})
//通过 wx.downloadFile 下载文件 生成临时存储文件
wx.downloadFile({
url: url,
success: function (res) {
const manage = wx.getFileSystemManager();
if (res.statusCode === 200) {
//保存临时文件
manage.saveFile({
tempFilePath: res.tempFilePath,
filePath: wx.env.USER_DATA_PATH + "/" + name + '.' + type,
success: function (res) {
console.log(res);
}
});
//打开文档(支持部分文件类型,详情查阅小程序开放文档-wx.openDocument)
wx.openDocument({
filePath: wx.env.USER_DATA_PATH + "/" + name + '.' + type,
fileType: type,
success: function (a) {
console.log(a);
return typeof 'function' && cb(a);
},
fail: function (a) {
wx.showToast({
title: '文件打开失败',
icon: 'none',
duration: 2000,
})
}
})
}
},
fail: function (res) {
return typeof 'function' && cb(res);
},
complete() {
wx.hideLoading({
success: (res) => {},
})
}
})
}
}
//文件下载
function downLoadFile({
path, //下载路径
name, //文件名
type, //文件类型()
system //手机系统类型
}) {
if (type == "jpg" || type == "png" || type == "jpeg") {
// var list = [path]
// console.log(path);
// wx.previewImage({
// current: path, // 当前显示图片的http链接
// urls: list // 需要预览的图片http链接列表
// })
downloadimg({
path,
name,
type
})
} else
if (type == 'mp4') {
// wx.navigateTo({
// url: '/pages/filesopen/filesopen?url=' + path,
// })
downloadmp4({
path,
name
})
} else {
//ios 端与 Android 存在差异
if (system.toUpperCase() == 'IOS') {
wx.showModal({
cancelText: '取消下载',
confirmText: '打开文件',
content: '打开文件后,点击右上角收藏到微信。',
success(res) {
if (res.confirm) {
wx.showLoading({
title: '加载中',
})
wx.downloadFile({
url: path,
success(res) {
console.log(res);
const gFileSystemManager = wx.getFileSystemManager();
if (res.statusCode === 200) {
//保存临时文件
gFileSystemManager.saveFile({
tempFilePath: res.tempFilePath,
filePath: wx.env.USER_DATA_PATH + "/" + name + '.' + type,
success: function (res) {
console.log(res);
}
});
wx.openDocument({
filePath: wx.env.USER_DATA_PATH + '/' + name + '.' + type,
fileType: type,
showMenu: true,
success: function (res) {
console.log(res);
return typeof 'function'
// console.log('打开文档成功')
},
fail(err) {
console.log(err);
console.log("打开失败");
}
})
} else if (res.cancel) {
console.log('用户点击取消')
}
},
fail(err) {
wx.showToast({
title: '文件打开失败',
icon: 'none',
duration: 2000,
})
},
complete(ook) {
wx.hideLoading()
}
})
}
}
})
} else {
wx.showLoading({
title: '下载中'
})
wx.downloadFile({
url: path,
success(res) {
const FileSystemManager = wx.getFileSystemManager()
FileSystemManager.saveFile({
tempFilePath: res.tempFilePath,
filePath: wx.env.USER_DATA_PATH + "/" + name + '.' + type,
success(res2) {
wx.showModal({
title: '下载成功',
content: "文件已存储此路径" + res2.savedFilePath
})
},
fail(err) {
console.log(err);
wx.showToast({
title: '文件打开失败',
icon: 'none',
duration: 2000,
})
},
complete(ook) {
wx.hideLoading()
}
})
}
})
}
}
}
//视频下载
function downloadmp4({
path,
name
}) {
console.log(wx.env, "userPath")
wx.showLoading({
title: '正在下载',
mask: true
})
wx.downloadFile({
url: path, // 视频地址 根据自己业务修改
filePath: wx.env.USER_DATA_PATH + '/' + name + '.mp4',
success: (res) => {
console.log(res)
wx.saveVideoToPhotosAlbum({
filePath: res.filePath,
success: (ress) => {
wx.showToast({
title: '下载成功',
icon: 'success'
})
//删除临时文件
var fileManager = wx.getFileSystemManager(); //全局唯一的文件管理器
fileManager.unlink({ //删除
filePath: wx.env.USER_DATA_PATH + '/' + name + '.mp4',
success: function (resf) {
console.log('unlink', resf)
}
})
},
fail: (ress) => {
wx.hideLoading()
console.log(ress)
if (ress.errMsg == 'saveVideoToPhotosAlbum:fail auth deny') {
wx.showModal({
title: '提示',
content: '需要您授与xxx"相册"权限', // 提示语, 根据业务修改
showCancel: false,
success: function (c) {
//调起客户端小程序设置界面,返回用户设置的操作结果。
wx.openSetting({
success: function (settingdata) {
if (settingdata.authSetting['scope.writePhotosAlbum']) {
wx.showModal({
title: "提示",
content: "获取权限成功,再次点击下载即可保存",
showCancel: false
})
} else {
wx.showModal({
title: "提示",
content: "获取权限失败,将无法保存到相册",
showCancel: false
})
}
}
})
}
})
}
}
})
},
fail(err) {
console.log(err);
}
})
}
//图片下载
function downloadimg({
path,
name,
type
}) {
console.log(wx.env, "userPath")
wx.showLoading({
title: '正在下载',
mask: true
})
wx.downloadFile({
url: path, // 视频地址 根据自己业务修改
filePath: wx.env.USER_DATA_PATH + '/' + name + '.' + type,
success: (res) => {
console.log(res)
wx.saveImageToPhotosAlbum({
filePath: res.filePath,
success: (ress) => {
wx.showToast({
title: '下载成功',
icon: 'success'
})
//删除临时文件
var fileManager = wx.getFileSystemManager(); //全局唯一的文件管理器
fileManager.unlink({ //删除
filePath: wx.env.USER_DATA_PATH + '/' + name + '.' + type,
success: function (resf) {
console.log('unlink', resf)
}
})
},
fail: (ress) => {
wx.hideLoading()
console.log(ress)
if (ress.errMsg == 'saveVideoToPhotosAlbum:fail auth deny') {
wx.showModal({
title: '提示',
content: '需要您授与xxx"相册"权限', // 提示语, 根据业务修改
showCancel: false,
success: function (c) {
//调起客户端小程序设置界面,返回用户设置的操作结果。
wx.openSetting({
success: function (settingdata) {
if (settingdata.authSetting['scope.writePhotosAlbum']) {
wx.showModal({
title: "提示",
content: "获取权限成功,再次点击下载即可保存",
showCancel: false
})
} else {
wx.showModal({
title: "提示",
content: "获取权限失败,将无法保存到相册",
showCancel: false
})
}
}
})
}
})
}
}
})
},
fail(err) {
console.log(err);
}
})
}
【微信小程序文件预览/下载(图片/视频/文件)】
最新推荐文章于 2024-05-07 10:58:33 发布