小程序-下载文件的方式
正常下载的时候,出现下载的时候空白,基本是格式出现问题,或者是文件名出现问题。文件不能是中文
写法
const downloadFile = (interfaceName, fileName, success, params) => { wx.showLoading({ title: '下载中' }); let originURL = httpUrl + interfaceName // '你的接口名'; if(params) originURL += params; let filePath = `${wx.env.USER_DATA_PATH}/${fileName}`; getOpenId(interfaceName,function () { // 没有验证方法也可以 wx.downloadFile({ url: originURL, filePath: filePath, header: { 'sessionId': getStorage('u_token'), }, success: (res) => { wx.hideLoading(); if(res.statusCode == 200) { if (success) { success(res); } } }, fail: (res) => { wx.showToast({ title: '下载失败', icon: 'none', }); }, }); }) }; exportFiles: function() { const that = this; const url = '你的接口名'; const data = this.data;
const getTimes = (new Date()).getTime(); const fileName = getTimes+'文件名.pdf'; // 文件格式错误,会出现接口红白,但是下载文件打开错误 const params = `?getTimes=${getTimes}` https.downloadFile(url, fileName, (res) => { fileValue.data = res.filePath; that.setData({ fileValue, }); that.onSaveDocument(res.filePath, () => { wx.openDocument({ filePath: res.filePath, showMenu: true, }); }); }, params ) }, onSaveDocument(pathval, callback) { wx.getSystemInfo({ success: function (res) { if (res.platform === 'windows') { wx.saveFileToDisk({ filePath: pathval, }) } else callback() } }); },
如需转载原创文章,请标注原文地址,版权所有!
浙公网安备 33010602011771号