使用h5+ API实现文件下载,APP和webview均可使用
uni.showLoading()
var name = '承诺书模板.xls'; //文件名称可以在上传时进行保存,下载时取出,当文件名称中存在单双引号时,要做好处理,否则会报错
var url = 'http://web.gdut.edu.cn/~jsxy/doc/keshitongji.xls'; //文件名称可以在上传时进行保存,下载时取出,当文件名称中存在单双引号时,要做好处理,否则会报错,路径大家根据需求替换,这是我自己网上找的demo
var dtask = plus.downloader.createDownload(url,{
filename:"_downloads/"+name //利用保存路径,实现下载文件的重命名
},function(d,status){
uni.hideLoading()
console.log('d', d, status)
//d为下载的文件对象
if(status==200){
console.log('下载成功')
//下载成功,d.filename是文件在保存在本地的相对路径,使用下面的API可转为平台绝对路径
this.localPath = plus.io.convertLocalFileSystemURL(d.filename);
plus.runtime.openFile(d.filename); //选择软件打开文件
}else{
console.log('下载失败')
//下载失败
plus.downloader.clear(); //清除下载任务
}
})
dtask.start();
使用uniapp的API实现文件下载
var _this = this
uni.downloadFile({
url: 'http://web.gdut.edu.cn/~jsxy/doc/keshitongji.xls',
success: function (res) {
var path = res.tempFilePath
console.log('res', res);
if (res.statusCode === 200) {
// preview
uni.openDocument({
filePath: path,
success: function(res) {
console.log('打开文档成功');
console.log(res);
}
});
//文件保存
uni.saveFile({
tempFilePath: path,
success: function(res) {
_this.localPath = res.savedFilePath;
console.log(savedFilePath)
uni.showToast({
title: '下载成功',
content: savedFilePath,
duration: 500
});
uni.getSavedFileList({
success: function(res) {
console.log(res.fileList);
}
});
}
});
}
},
fail(err){
console.log('下载失败', err)
}
});