アプリとモバイルのダウンロード用のコード スニペット

 最初

uni.downloadFile({
    url: this.http.urls.inventory_my_export + '?userId=' + this.getUser().id,
	header: { Authorization: this.getUser().token },
	success: res => {
		uni.hideLoading();
		uni.saveFile({
			tempFilePath: res.tempFilePath, //临时路径
			success: res => {
				uni.showModal({
					title: '提示',
					content: '清单已导出:' + res.savedFilePath,
					cancelText: '我知道了',
					confirmText: '打开文件',
					success: r => {
						if (r.confirm) {
							uni.openDocument({
								filePath: res.savedFilePath,
								showMenu: true,
								success: res => {}
							});
						}
					}
				});
			}
		});
	},
	fail: err => {
		uni.showModal({ content: '导出文件失败', showCancel: false });
	}
});

二番目

uni.downloadFile({
    url: url, //仅为示例,并非真实的资源
	success: (response) => {
	    console.log('response',response)
		if (response.statusCode === 200) {
		    console.log('下载成功');
			// 保存文件到本地
			uni.saveFile({
			    empFilePath: response.tempFilePath,
			    success: (resData) => {
			        console.log('saveFile =====> resData',resData);
			        // 新开页面打开文档,支持格式:doc, xls, ppt, pdf, docx, xlsx, pptx。
			        uni.openDocument({
			            filePath: resData.savedFilePath,
			            fileType: 'pdf', // 指定文件的格式
			            showMenu: true, // 允许出现分享功能
			            success: r => {
			               console.log('openDocument ===> res',r)
			            },
			            fail: openError => {
			               console.log('打开失败: ', openError)
			            }
			        })
			    },
			    fail: error => {
			        console.log('error: ', error)
			    }
			})
		}
	}
});

おすすめ

転載: blog.csdn.net/qq_52421092/article/details/132042272