接口通过网页浏览器访问是能够正常下载一个xlsx类型的文件。但是通过调用uni.downloadFile方法下载该文件时候,临时文件没有带上后缀,而且文件名称还是接口名称。 

uni.downloadFile({
					url: 'https://www.example.com/file/test', //仅为示例,并非真实的资源
					responseType: 'blob',
					success: (res) => {
						if (res.statusCode === 200) {
							console.log('下载成功');
                            //更改文件名称
							// 拿到临时文件的绝对路径
							let filepathss = plus.io.convertLocalFileSystemURL(res.tempFilePath);
							// 通过这个路径来拿到他
							plus.io.resolveLocalFileSystemURL(filepathss, function(entry) {
								const tempFileName = entry.name;
								entry.getParent(function(parentDicEntry) {
									console.log(parentDicEntry.fullPath)
									entry.moveTo({
										fullPath: parentDicEntry.fullPath + '/'
									}, '新的文件名字.xlsx', function(newFile) {
										uni.openDocument({
											fileType: 'xlsx',
											filePath: newFile.fullPath,
											success: function(res2) {
												console.log('打开文档成功');
											},
											fail: function(err) {
												if (err.errCode == 1 || err
													.code == 1) {
													uni.showToast({
														title: `未找到能打开xlsx类型文件的手机应用`,
														icon: 'none'
													})
												}
												console.log(err)
											}
										});
									}, function(moveError) {
										uni.showToast({
											title: `已在第三方应用中打开过,请在第三方应用查看时保存`,
											icon: 'none'
										})
									})
								})
							})
						}
					}
				});
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐