小程序访问服务器PDF,小程序打开pdf文件并保存到本地 支持远程pdf、word、xls表格文件打开...

该博客介绍了如何在uni-app小程序中处理包含中文名的文件下载和预览。通过使用`encodeURI`和`escape`方法解决网络地址中中文文件名的问题,实现了下载文件并根据文件类型进行预览或保存的功能。主要涉及uni-app的downloadFile和saveFile API的使用。
摘要由CSDN通过智能技术生成

小程序支持打开文件 doc, xls, ppt, pdf, docx, xlsx, pptx

利用encodeURI与escape,兼容处理了传输过来的文件带有中文名会出现的问题

点击下载文件

保存

e2c2cc2b177ff94ae0dd15ce4ef32073.png

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();

}

});

},

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值