示例源码如下,请查看 pre > code 标签中的内容
<template>
<!-- #ifdef APP -->
<scroll-view style="flex: 1">
<!-- #endif -->
<view>
<page-head :title="data.title"></page-head>
<view>
<view v-if="data.imageSrc">
<image class="img" :src="data.imageSrc" mode="aspectFit" />
</view>
<view v-else style="margin: 10px;">
<text class="uni-hello-text">点击按钮下载服务端示例图片(下载网络文件到本地临时目录)</text>
<button type="primary" @tap="downloadImage">下载</button>
</view>
<view style="margin: 10px;">
<text class="uni-hello-text">下载接口的Content-Disposition中的filename非法值例子</text>
<button type="primary" @tap="downloadErrorFilename">下载</button>
</view>
</view>
</view>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<script setup lang="uts">
// #ifdef APP-ANDROID || APP-IOS || APP-HARMONY
import {
testInovkeDownloadFile,
CommonOptions
} from '@/uni_modules/test-invoke-network-api'
// #endif
type DataType = {
title: string;
imageSrc: string;
task: DownloadTask | null;
jest_result: boolean;
jest_callback_triggred: boolean;
}
const data = reactive({
title: 'downloadFile',
imageSrc: '',
task: null,
//自动化测试例专用
jest_result: false,
jest_callback_triggred: false
} as DataType)
onUnload(() => {
// data.imageSrc = '';
uni.hideLoading();
data.task?.abort();
})
const downloadImage = () => {
uni.showLoading({
title: '下载中'
})
data.task = uni.downloadFile({
url: "https://web-assets.dcloud.net.cn/hello-uni-app-x/uni-app.png",
success: (res) => {
console.log('downloadFile success, res is', res.tempFilePath)
data.imageSrc = res.tempFilePath;
},
fail: (err) => {
console.log('downloadFile fail, err is:', err)
},
complete: (res) => {
uni.hideLoading();
data.task = null;
}
});
data.task?.onProgressUpdate((update) => {
console.log("progress : ", update.progress);
})
}
const downloadErrorFilename = () => {
data.task = uni.downloadFile({
url:"https://qiniu-web-assets.dcloud.net.cn/uni-app-x/static/file/test9.txt",
success: (res) => {
console.log('downloadFile success, res is', res.tempFilePath)
},
fail: (err) => {
console.log('downloadFile fail, err is:', err)
}
})
}
//自动化测试例专用
const jest_downloadFile = () => {
data.task = uni.downloadFile({
url: "https://web-assets.dcloud.net.cn/hello-uni-app-x/uni-app.png",
success: () => {
data.jest_result = true
data.jest_callback_triggred = true
},
fail: () => {
data.jest_result = false
data.jest_callback_triggred = true
}
});
}
const jest_downloadFile_with_uni_env = () => {
data.task = uni.downloadFile({
url: "https://web-assets.dcloud.net.cn/hello-uni-app-x/uni-app.png",
filePath: `${uni.env.CACHE_PATH}/a/b/`,
success: () => {
data.jest_result = true
data.jest_callback_triggred = true
},
fail: () => {
data.jest_result = false
data.jest_callback_triggred = true
}
});
}
const jest_cookie_download = (needCookie : boolean) => {
data.task = uni.downloadFile({
url: "https://request.dcloud.net.cn/api/http/header/download",
success: () => {
data.jest_result = needCookie ? true : false;
data.jest_callback_triggred = true
},
fail: () => {
data.jest_result = needCookie ? false : true;
}
});
}
const jest_set_cookie = () => {
uni.request({
url: "https://request.dcloud.net.cn/api/http/header/setCookie",
method: "GET",
timeout: 6000,
sslVerify: false,
withCredentials: true,
firstIpv4: false,
success: () => {
jest_cookie_download(true)
},
fail: () => {
data.jest_result = false;
data.jest_callback_triggred = true
},
});
}
const jest_delete_cookie = () => {
uni.request({
url: "https://request.dcloud.net.cn/api/http/header/deleteCookie",
method: "GET",
timeout: 6000,
sslVerify: false,
withCredentials: true,
firstIpv4: false,
success: () => {
jest_cookie_download(false)
},
fail: () => {
data.jest_result = false;
data.jest_callback_triggred = true
},
});
}
const jest_uts_module_invoked = () => {
// #ifdef APP-ANDROID || APP-IOS || APP-HARMONY
testInovkeDownloadFile({
success: (res : any) => {
data.jest_result = true
data.jest_callback_triggred = true
},
fail: (err : any) => {
data.jest_result = false
data.jest_callback_triggred = true
}
} as CommonOptions)
// #endif
}
const jest_special_characters_download = () => {
data.task = uni.downloadFile({
url: "https://web-assets.dcloud.net.cn/hello-uni-app-x/1789834995055525889-你好%23你好.png",
success: () => {
data.jest_result = true;
data.jest_callback_triggred = true
},
fail: () => {
data.jest_result = false;
data.jest_callback_triggred = true
}
});
}
const jest_download_call_timeout = () => {
data.task = uni.downloadFile({
url: "https://web-assets.dcloud.net.cn/video/sample/2minute-demo-10k.mp4",
timeout: 3000,
fail: () => {
data.jest_result = false;
}
})
setTimeout(() => {
data.jest_result = true;
}, 4000)
}
const jest_download_long_url = () => {
data.task = uni.downloadFile({
url: "https://request.dcloud.net.cn/WsQkvvoLPbLFRoZB3NZOhgR8bMbjvpMZMmnzhlH1UtAzbVaOqokI9NBhqgc0pUVesJVQeJoTXjCbuAHqsoGJryxzjo4xf2bR75GTfOgJ5mfDDoX31DY9HTQ0mgLwTlt43MWsRobR22r6qwJYRaZW4yLthUX1BSzckZS9OeKzyV2r3dizxkbAPV3j5toN5qjUq5gYJOHbWfOEdwCA6tftgEGzc3eb6Deed0ngLtNnYYcVOWCzXk24jhYP87g9yh1o9kowdQXUMv4mhwxbvvulLTDC470HqLT1cLm7iuh38L1c8EdMJ6xSCO8y1mxKDzczA0W29mzf54jUfuO7OodX9ZJeFQhCptm73P8oSzyc57BaP9zecVN0u1LwPa4cyj1vMleUSfI2lSzT5PIt2YCojkFEunuFWHRq5vtDQ2ZXQGYIJqSmBDDsVXMAlHAC91F1uWsuC67GgcAPMtrXAyB8HbCA4fiP9iYsKmkKKwMUFx04ehsabTuWnOO6Oms14wrqghKGUJKKXdCaYomcCrRDRUOjIm3NwRKqUN1cHiHI9cxXQbWD8FFb8qBEPrrFscog2dHhQyM8nBbdrk9Lc99ao7bE1s39qiYxSZPnSdfHVb0zfgg9vkm0DTd47pTHz3qOhtWLgNLBLTsCf3HMVG4yZgGcenX3XG2Z6OgG6Pm1DsimJfZtzYU78Cbbp395mdnX6bj3bNNzraV6JVLLiI6diJwtdipIMBFle0t0S0eWMNSGUiZHDwjVTGibHK4PTSd6VkJbBcxSxdmdApn1LiSmxJTciH0auKrSzriVxjDGyQ2bnqyStG",
success: () => {
data.jest_result = true;
data.jest_callback_triggred = true
},
fail: () => {
data.jest_result = false;
data.jest_callback_triggred = true
}
});
}
defineExpose({
data,
jest_downloadFile,
jest_downloadFile_with_uni_env,
jest_set_cookie,
jest_delete_cookie,
jest_uts_module_invoked,
jest_special_characters_download,
jest_download_call_timeout,
jest_download_long_url
})
</script>
<style>
.img {
margin: 0 auto;
}
</style>