download-file.uvue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view style="flex: 1">
  4. <!-- #endif -->
  5. <view>
  6. <page-head :title="title"></page-head>
  7. <view>
  8. <view v-if="imageSrc">
  9. <image class="img" :src="imageSrc" mode="aspectFit" />
  10. </view>
  11. <view v-else style="margin: 10px;">
  12. <text class="uni-hello-text">点击按钮下载服务端示例图片(下载网络文件到本地临时目录)</text>
  13. <button type="primary" @tap="downloadImage">下载</button>
  14. </view>
  15. <view style="margin: 10px;">
  16. <text class="uni-hello-text">下载接口的Content-Disposition中的filename非法值例子</text>
  17. <button type="primary" @tap="downloadErrorFilename">下载</button>
  18. </view>
  19. </view>
  20. </view>
  21. <!-- #ifdef APP -->
  22. </scroll-view>
  23. <!-- #endif -->
  24. </template>
  25. <script>
  26. // #ifdef APP-ANDROID || APP-IOS || APP-HARMONY
  27. import {
  28. testInovkeDownloadFile,
  29. CommonOptions
  30. } from '@/uni_modules/test-invoke-network-api'
  31. // #endif
  32. export default {
  33. data() {
  34. return {
  35. title: 'downloadFile',
  36. imageSrc: '',
  37. task: null as DownloadTask | null,
  38. //自动化测试例专用
  39. jest_result: false,
  40. jest_callback_triggred: false
  41. }
  42. },
  43. onLoad() {
  44. },
  45. onUnload() {
  46. // this.imageSrc = '';
  47. uni.hideLoading();
  48. this.task?.abort();
  49. },
  50. methods: {
  51. downloadImage: function () {
  52. uni.showLoading({
  53. title: '下载中'
  54. })
  55. var self = this
  56. this.task = uni.downloadFile({
  57. url: "https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni-app.png",
  58. success: (res) => {
  59. console.log('downloadFile success, res is', res.tempFilePath)
  60. self.imageSrc = res.tempFilePath;
  61. },
  62. fail: (err) => {
  63. console.log('downloadFile fail, err is:', err)
  64. },
  65. complete: (res) => {
  66. uni.hideLoading();
  67. this.task = null;
  68. }
  69. });
  70. this.task?.onProgressUpdate((update) => {
  71. console.log("progress : ", update.progress);
  72. })
  73. },
  74. downloadErrorFilename(){
  75. uni.downloadFile({
  76. url:"https://qiniu-web-assets.dcloud.net.cn/uni-app-x/static/file/test9.txt",
  77. success: (res) => {
  78. console.log('downloadFile success, res is', res.tempFilePath)
  79. },
  80. fail: (err) => {
  81. console.log('downloadFile fail, err is:', err)
  82. }
  83. })
  84. },
  85. //自动化测试例专用
  86. jest_downloadFile() {
  87. uni.downloadFile({
  88. url: "https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni-app.png",
  89. success: () => {
  90. this.jest_result = true
  91. this.jest_callback_triggred = true
  92. },
  93. fail: () => {
  94. this.jest_result = false
  95. this.jest_callback_triggred = true
  96. }
  97. });
  98. },
  99. jest_downloadFile_with_uni_env() {
  100. uni.downloadFile({
  101. url: "https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni-app.png",
  102. filePath: `${uni.env.CACHE_PATH}/a/b/`,
  103. success: () => {
  104. this.jest_result = true
  105. this.jest_callback_triggred = true
  106. },
  107. fail: () => {
  108. this.jest_result = false
  109. this.jest_callback_triggred = true
  110. }
  111. });
  112. },
  113. jest_set_cookie() {
  114. uni.request({
  115. url: "https://request.dcloud.net.cn/api/http/header/setCookie",
  116. method: "GET",
  117. timeout: 6000,
  118. sslVerify: false,
  119. withCredentials: true,
  120. firstIpv4: false,
  121. success: () => {
  122. this.jest_cookie_download(true)
  123. },
  124. fail: () => {
  125. this.jest_result = false;
  126. this.jest_callback_triggred = true
  127. },
  128. });
  129. },
  130. jest_delete_cookie() {
  131. uni.request({
  132. url: "https://request.dcloud.net.cn/api/http/header/deleteCookie",
  133. method: "GET",
  134. timeout: 6000,
  135. sslVerify: false,
  136. withCredentials: true,
  137. firstIpv4: false,
  138. success: () => {
  139. this.jest_cookie_download(false)
  140. },
  141. fail: () => {
  142. this.jest_result = false;
  143. this.jest_callback_triggred = true
  144. },
  145. });
  146. },
  147. jest_cookie_download(needCookie : boolean) {
  148. uni.downloadFile({
  149. url: "https://request.dcloud.net.cn/api/http/header/download",
  150. success: () => {
  151. this.jest_result = needCookie ? true : false;
  152. this.jest_callback_triggred = true
  153. },
  154. fail: () => {
  155. this.jest_result = needCookie ? false : true;
  156. }
  157. });
  158. },
  159. jest_uts_module_invoked() {
  160. // #ifdef APP-ANDROID || APP-IOS || APP-HARMONY
  161. testInovkeDownloadFile({
  162. success: (res : any) => {
  163. this.jest_result = true
  164. this.jest_callback_triggred = true
  165. },
  166. fail: (err : any) => {
  167. this.jest_result = false
  168. this.jest_callback_triggred = true
  169. }
  170. } as CommonOptions)
  171. // #endif
  172. },
  173. jest_special_characters_download() {
  174. uni.downloadFile({
  175. url: "https://web-ext-storage.dcloud.net.cn/hello-uni-app-x/1789834995055525889-你好%23你好.png",
  176. success: (res : DownloadFileSuccess) => {
  177. this.jest_result = true;
  178. this.jest_callback_triggred = true
  179. },
  180. fail: () => {
  181. this.jest_result = false;
  182. this.jest_callback_triggred = true
  183. }
  184. });
  185. },
  186. jest_download_call_timeout() {
  187. uni.downloadFile({
  188. url: "https://web-assets.dcloud.net.cn/video/sample/2minute-demo-10k.mp4",
  189. timeout: 3000,
  190. fail: () => {
  191. this.jest_result = false;
  192. }
  193. })
  194. setTimeout(() => {
  195. this.jest_result = true;
  196. }, 4000)
  197. }
  198. }
  199. }
  200. </script>
  201. <style>
  202. .img {
  203. margin: 0 auto;
  204. }
  205. </style>