Funktion zum Herunterladen von Vorlagen für Uniapp-Entwicklungs-Apps

Die geschäftliche Anforderung besteht darin, dass Sie beim Einreichen verschiedener Anträge auf dem mobilen Endgerät die angegebene bereitgestellte Vorlage herunterladen und absenden müssen. Wenn es jedoch in eine App gepackt ist, ist es nicht einfach, die entsprechende Download-Datei durch Ausführen von uni.downloadFile zu finden. Laut Internet wird ein weiterer Download durchgeführt.

So wurde es vorher geschrieben:

			// #ifdef APP-PLUS
			var that = this;
			uni.downloadFile({
    
    
				url: `${
      
      locals}/Template/${
      
      that.templateFile}`,
				header: {
    
    
			 		token: uni.getStorageSync('token')
				},
			 	success: data => {
    
    
			 		uni.saveFile({
    
    
			 			//文件保存到本地
			 			tempFilePath: data.tempFilePath, //临时路径
			 			success: function(res) {
    
    
			 				that.$refs.uToast.show({
    
    
			 					...that.$GetMssage(true, '文件已保存')
			 				});
			 				setTimeout(() => {
    
    
			 					uni.openDocument({
    
    
			 						filePath: res.savedFilePath,
			 						success: function(res) {
    
    
			 							console.log('打开文档成功');
			 						}
			 					});
			 				}, 2000);
			 			}
			 		});
			 	}
			});
			// #endif

Dies ist eine neue Schreibmethode nach Abfrage der Informationen, die bei persönlichen Tests wirksam ist.

		// #ifdef APP-PLUS
		var that = this;
		var url = `${
      
      locals}/Template/${
      
      that.templateFile}`;
		let dtask = plus.downloader.createDownload(
			url,
			{
    
    
				//本地路径开头使用file://,跟上手机文件本地目录storage/emulated/0,就是用户文件管理器能看到的了,之后我创建微垠作为文件夹,后缀是用于文件命名和格式修改,大家可以使用变量。
				// ***这块算是自己新建一个文件夹的名字,后面跟的文件名字
				filename: 'file://storage/emulated/0/***/' + that.templateFile //利用保存路径,实现下载文件的重命名
			},
			function(d, status) {
    
    
				//d为下载的文件对象
				if (status == 200) {
    
    
					//下载成功,d.filename是文件在保存在本地的相对路径,使用下面的API可转为平台绝对路径
					// let fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename);
					// plus.runtime.openFile(d.filename); //选择软件打开文件
					// 上面这个打开文件,好像是因为本地没有下载对应wps等软件,所以这点修改成一个提示,而非自动打开了。
					that.$refs.uToast.show({
    
    
						...that.$GetMssage(true, '模板下载成功')
					});
				} else {
    
    
					//下载失败
					plus.downloader.clear(); //清除下载任务
				}
			}
		);
		dtask.start();
		// #endif

おすすめ

転載: blog.csdn.net/oldolder/article/details/127867750