微信小程序预览文件

js代码

showloadFile: function(e) {

wx.showLoading({

      title: '加载中...',

});

let url = e.currentTarget.dataset.info;//线上文件路径

//判断文件类型

let ext = url.substr(url.lastIndexOf(".")).replace('.', '');

if (ext != 'doc' && ext != 'docx' && ext != 'xls' && ext != 'xlsx' && ext != 'ppt' && ext != 'pptx' && ext != 'pdf') {

wx.hideLoading();

app.showToast({

title: '错误的文件格式',

icon: 'none'

})

};

//预下载文件

wx.downloadFile({

// e.target.dataset.name 是文件的地址

url: url,

header: {},

fileType: types,

success(res) {

let filePath = res.tempFilePath;//预下载路径

// console.log(res)

//打开文件

wx.openDocument({

filePath: filePath,

success(res) {

wx.hideLoading();

// console.log('打开文档成功')

}

})

}

})

},

你可能感兴趣的:(微信小程序开发)