
支持打开文件 doc, xls, ppt, pdf, docx, xlsx, pptx
利用encodeURI与escape,兼容处理了传输过来的文件带有中文名会出现的问题
<view @tap="file_yulan('')">点击下载文件</view>
<view @tap="file_save('')">保存</view>
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
//url: escape(filePath), //escape(filePath) 另外一种可能出现问题处理方式,安卓打开成功,iOS打开失败【原因:打开的文件的文件名是中文】
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;
//打开文件有效值 doc, xls, ppt, pdf, docx, xlsx, pptx
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();
}
});
},
您发布的评论即表示同意遵守以下条款:
一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家、社会、集体和公民的合法权益;
二、不得发布国家法律、法规明令禁止的内容;互相尊重,对自己在本站的言论和行为负责;
三、本站对您所发布内容拥有处置权。