微信小程序踩坑:wx.openDocument(Object object)打不开文件

官网:

wx.openDocument(Object object) | 微信开放文档微信开发者平台文档https://developers.weixin.qq.com/miniprogram/dev/api/file/wx.openDocument.html
说的fileType是可选参数!

 但是不加上的话,就打不开!

加上之后就能打开了。

 关键代码:

	wx.openDocument({
									filePath: filePath,
									fileType: fileType,
									showMenu: true,
									success: function(res) {
										console.log('打开文档成功');
									},
									fail: function(error) {
										wx.showToast({
											icon: 'none',
											title: '打开文件失败'
										});
									},
								});

测试数据:先从文件的url里面筛选文件格式:

let ss='https://www.baidu.com/f85284da55164de2a2d9cb0b62fabab1.pdf?response-content-disposition=attachment%3Bfilename%3D%E6%9D%A8%E5%AD%90%E5%B9%BF%E5%91%8A%E5%87%BA%E8%B4%A7%E5%8D%95.pdf&AWSAccessKeyId=XIDM2OFRLLDUR8YFJJWM&Expires=1679793304&Signature=WcNl5HmwWOvNU%2F51f%2F5ZQ1h9BbA%3D'
ss.substring(ss.lastIndexOf(".")+1).split('&')[0]
'pdf'

demo: 

	/**
			 * 微信小程序只支持fileType这些类型:
			 *       doc  doc 格式
			 *         docx  docx 格式
			 *         xls  xls 格式
			 *         xlsx  xlsx 格式
			 *         ppt  ppt 格式
			 *         pptx  pptx 格式
			 *         pdf  pdf 格式
			 * @param url
			 */
			downloadFile(url) {
url='https://www.baidu.com/f85284da55164de2a2d9cb0b62fabab1.pdf?response-content-disposition=attachment%3Bfilename%3D%E6%9D%A8%E5%AD%90%E5%B9%BF%E5%91%8A%E5%87%BA%E8%B4%A7%E5%8D%95.pdf&AWSAccessKeyId=XIDM2OFRLLDUR8YFJJWM&Expires=1679793304&Signature=WcNl5HmwWOvNU%2F51f%2F5ZQ1h9BbA%3D'
				console.log('url', '---的值是?', url)

				const sufInx = url.lastIndexOf(".");

				if (sufInx === -1) {

					uni.showModal({
						title: '提示信息',
						content: '文件错误!',
						success: function(res) {
							if (res.confirm) {
								//console.log('用户点击确定');
							} else if (res.cancel) {
								//console.log('用户点击取消');
							}
						}
					});
					return false
				}
				let fileType = ''
				console.log('sufInx', '---的值是?', sufInx)
				fileType = url.substring(sufInx + 1).split('&')[0]

				console.log('fileType', '---的值是?', fileType)
				let fileTypeArr = ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf']

				console.log('fileType', '---的值是?', fileType)

				if (!fileTypeArr.includes(fileType)) {
					uni.showModal({
						title: '提示信息',
						content: '只能打开word/excel/ppt/pdf文件(比如:doc, docx, xls, xlsx, ppt, pptx, pdf这些类型的文件)!',
						success: function(res) {
							if (res.confirm) {
								//console.log('用户点击确定');
							} else if (res.cancel) {
								//console.log('用户点击取消');
							}
						}
					});
					return false
				} else {

					wx.downloadFile({
						url: url, //仅为示例,并非真实的资源
						success: (res) => {
							if (res.statusCode === 200) {
								console.log('下载成功--res', res);
								var filePath = res.tempFilePath;
								wx.openDocument({
									filePath: filePath,
									fileType: fileType,
									showMenu: true,
									success: function(res) {
										console.log('打开文档成功');
									},
									fail: function(error) {
										wx.showToast({
											icon: 'none',
											title: '打开文件失败'
										});
									},
								});
							}
						},
						fail: function(err) {
							console.log('fail')
							console.log(err)
							wx.showToast({
								icon: 'none',
								title: '下载文件失败'
							});
						}
					});
				}

			},


最后,pdf打开成功:

 

 

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
如果您不想使用 `uni.openDocument` API 打开文件,您可以考虑使用其他方式打开文件,比如使用第三方插件或者自定义组件等。 以下是两个示例: 1. 使用第三方插件: 可以使用 uni-app 社区中的一些第三方插件来实现打开文件的功能,例如 `uni-file-picker` 插件。这个插件可以选择文件并返回文件的本地路径,您可以使用这个路径来打开文件。 安装插件: ``` npm install uni-file-picker --save ``` 使用插件: ```javascript import filePicker from 'uni-file-picker' // 选择文件 filePicker.chooseFile({ success: (res) => { // res.tempFilePaths 为文件的本地临时路径 // 这里可以使用自己的方式打开文件 } }) ``` 2. 自定义组件: 您可以自定义一个组件来实现打开文件的功能。在组件中,可以使用 `wx.chooseMessageFile` API 选择文件并返回文件的临时路径,然后使用 `wx.openDocument` API 打开文件。 组件示例: ```html <template> <button @click="openFile">打开文件</button> </template> <script> export default { methods: { openFile() { wx.chooseMessageFile({ count: 1, type: 'file', success: (res) => { wx.openDocument({ filePath: res.tempFiles[0].path, fileType: 'txt', success: () => { console.log('打开文件成功') }, fail: () => { console.log('打开文件失败') } }) }, fail: () => { console.log('选择文件失败') } }) } } } </script> ``` 注意:在使用 `wx.chooseMessageFile` 和 `wx.openDocument` API 时,需要将 `type` 和 `fileType` 参数设置为要打开的文件类型,这里以打开 txt 文件为例,需要将类型设置为 `file`,文件类型设置为 `txt`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南北极之间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值