小程序支持打开文件 doc, xls, ppt, pdf, docx, xlsx, pptx
利用encodeURI与escape,兼容处理了传输过来的文件带有中文名会出现的问题
点击下载文件
保存
file_save: function(burl) {
console.log("bbbb");
uni.showLoading({
title: '文件下载中'
});
let fileType = "PNG"; //文件类型
let filePath = "http://192.168.0.110/UploadFiles/demo.pdf";
//把字符串转换为小写
if (fileType) {
fileType = fileType.toLowerCase();
}
//先下载文件
uni.downloadFile({
url: encodeURI(filePath), //注意中文文件名的网络地址需要encodeURI
success(res) {
console.log(res);
uni.hideLoading();
uni.saveFile({
tempFilePath: res.tempFilePath,
success: function(res) {
console.log(res);
var savedFilePath = res.savedFilePath;
}
});
//预览图片或打开文件
// if(fileType == "png" || fileType == "jpg" || fileType == "jpeg"){
// // 图片的话直接预览
// uni.previewImage({
// urls: [res.tempFilePath]
// });
// uni.hideLoading();
// }else{
// uni.openDocument({
// filePath: res.tempFilePath,
// success() {
// //打开文件中
// uni.showLoading({
// title: '打开文件中'
// });
// },
// fail(e) {
// uni.hideLoading();
// uni.showToast({
// icon: 'none',
// title: '文件打开失败!'
// });
// }
// });
//}
},
fail() {
uni.hideLoading();
uni.showToast({
icon: 'none',
title: '下载出错!'
});
},
complete() {
uni.hideLoading();
}
});
},
file_yulan: function(burl) {
console.log("处理下载文件中");
uni.showLoading({
title: '文件下载中'
});
let fileType = "PNG"; //文件类型
let filePath = "http://192.168.0.110/UploadFiles/demo.pdf";
//把字符串转换为小写
if (fileType) {
fileType = fileType.toLowerCase();
}
//先下载文件
uni.downloadFile({
url: encodeURI(filePath), //注意中文文件名的网络地址需要encodeURI
success(res) {
console.log(res);
uni.hideLoading();
//预览图片或打开文件
if (fileType == "png" || fileType == "jpg" || fileType == "jpeg") {
//图片的话直接预览
uni.previewImage({
urls: [res.tempFilePath]
});
uni.hideLoading();
} else {
//保存文件
uni.saveFile({
tempFilePath: res.tempFilePath,
success: function(res) {
console.log(res);
//var savedFilePath = res.savedFilePath;
uni.openDocument({
filePath: res.savedFilePath,
success() {
//打开文件中
uni.showLoading({
title: '打开文件中'
});
},
fail(e) {
uni.hideLoading();
uni.showToast({
icon: 'none',
title: '文件打开失败!'
});
}
});
}
});
}
},
fail() {
uni.hideLoading();
uni.showToast({
icon: 'none',
title: '下载出错!'
});
},
complete() {
uni.hideLoading();
}
});
},