uniapp常用

1.下载文件带进度提示

<template>

<view>

<button @click="startDownload">下载文件</button>

<progress :percent="progress" stroke-width="3" />

</view>

</template>

<script setup>

import {

ref

} from 'vue';

// 定义进度条数据

const progress = ref(0);

// 下载文件方法

const startDownload = () => {

const downloadTask = uni.downloadFile({

url: "http://localhost:5140/api/File/download", // 替换为你的API地址

success: (res) => {

console.log(res);

if (res.statusCode === 200) {

uni.showToast({

title: "下载成功",

icon: "success",

});

// 打开文件(根据文件类型选择打开方式)

uni.openDocument({

filePath: res.tempFilePath,

showMenu: true,

});

} else {

uni.showToast({

title: "下载失败",

icon: "none",

});

}

},

fail: (err) => {

console.error("下载失败", err);

uni.showToast({

title: "下载失败",

icon: "none",

});

},

});

// 监听下载进度

downloadTask.onProgressUpdate((res) => {

progress.value = res.progress; // 更新进度条

});

};

</script>

<style>

progress {

margin-top: 20px;

}

</style>

相关推荐
Jerry26 分钟前
使用 Material 3 在 Compose 中设置主题
前端
耶啵奶膘32 分钟前
uni-app头像叠加显示
开发语言·javascript·uni-app
chéng ௹33 分钟前
uniapp 封装uni.showToast提示
前端·javascript·uni-app
生擒小朵拉1 小时前
STM32添加库函数
java·javascript·stm32
tuokuac1 小时前
nginx配置前端请求转发到指定的后端ip
前端·tcp/ip·nginx
程序员爱钓鱼1 小时前
Go语言实战案例-开发一个Markdown转HTML工具
前端·后端·go
万少1 小时前
鸿蒙创新赛 HarmonyOS 6.0.0(20) 关键特性汇总
前端
吴传逞2 小时前
记一次uniapp+nutui-uniapp搭建项目
uni-app
还有多远.2 小时前
jsBridge接入流程
前端·javascript·vue.js·react.js
蝶恋舞者2 小时前
web 网页数据传输处理过程
前端