upload-file.uvue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view class="page-scroll-view">
  4. <!-- #endif -->
  5. <view>
  6. <page-head :title="title"></page-head>
  7. <view class="uni-padding-wrap uni-common-mt">
  8. <view class="demo">
  9. <image v-if="imageSrc" :src="imageSrc" class="image" mode="widthFix"></image>
  10. <text v-else class="uni-hello-addfile" @click="chooseImage">+ 选择图片</text>
  11. </view>
  12. </view>
  13. </view>
  14. <!-- #ifdef APP -->
  15. </scroll-view>
  16. <!-- #endif -->
  17. </template>
  18. <script>
  19. // #ifdef APP-ANDROID || APP-IOS || APP-HARMONY
  20. import {
  21. testInovkeUploadFile,
  22. CommonOptions
  23. } from '@/uni_modules/test-invoke-network-api'
  24. // #endif
  25. export default {
  26. data() {
  27. return {
  28. title: 'uploadFile',
  29. imageSrc: '',
  30. task: null as UploadTask | null,
  31. //自动化测试例专用
  32. jest_result: false,
  33. }
  34. },
  35. onLoad() {
  36. },
  37. onUnload() {
  38. this.imageSrc = '';
  39. uni.hideLoading();
  40. this.task?.abort();
  41. },
  42. methods: {
  43. chooseImage: function () {
  44. uni.chooseImage({
  45. count: 1,
  46. sizeType: ['compressed'],
  47. sourceType: ['album'],
  48. success: (res) => {
  49. console.log('chooseImage success, temp path is', res.tempFilePaths[0])
  50. var imageSrc = res.tempFilePaths[0]
  51. uni.showLoading({
  52. title: '上传中'
  53. })
  54. this.task = uni.uploadFile({
  55. url: 'https://unidemo.dcloud.net.cn/upload', //仅为示例,非真实的接口地址
  56. filePath: imageSrc,
  57. name: 'file',
  58. formData: {
  59. 'user': 'test'
  60. },
  61. success: (res) => {
  62. console.log('uploadImage success, res is:', res)
  63. uni.showToast({
  64. title: '上传成功',
  65. icon: 'success',
  66. duration: 1000
  67. })
  68. this.imageSrc = imageSrc
  69. },
  70. fail: (err) => {
  71. console.log('uploadImage fail', err);
  72. uni.showModal({
  73. content: err.errMsg,
  74. showCancel: false
  75. });
  76. },
  77. complete: (res) => {
  78. uni.hideLoading();
  79. this.task = null
  80. }
  81. });
  82. },
  83. fail: (err) => {
  84. console.log('chooseImage fail', err)
  85. }
  86. })
  87. },
  88. //自动化测试例专用
  89. jest_uploadFile() {
  90. const imageSrc = "/static/uni.png";
  91. uni.uploadFile({
  92. url: 'https://unidemo.dcloud.net.cn/upload', //仅为示例,非真实的接口地址
  93. filePath: imageSrc,
  94. name: 'file',
  95. formData: {
  96. 'user': 'test'
  97. },
  98. success: () => {
  99. this.jest_result = true;
  100. },
  101. fail: () => {
  102. this.jest_result = false;
  103. },
  104. })
  105. },
  106. jest_uploadFile_with_uni_env() {
  107. /**
  108. * 微信小程序只支持USER_DATA_PATH,且子目录未创建的情况下不能直接下载到子目录内
  109. */
  110. const filePath = `${uni.env.USER_DATA_PATH}/uni-app.png`
  111. uni.downloadFile({
  112. url: "https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/uni-app.png",
  113. filePath: filePath,
  114. success: () => {
  115. uni.uploadFile({
  116. url: 'https://unidemo.dcloud.net.cn/upload', //仅为示例,非真实的接口地址
  117. filePath: filePath,
  118. name: 'file',
  119. success: () => {
  120. this.jest_result = true;
  121. },
  122. fail: () => {
  123. this.jest_result = false;
  124. },
  125. })
  126. },
  127. fail: () => {
  128. this.jest_result = false
  129. }
  130. });
  131. },
  132. jest_set_cookie() {
  133. uni.request({
  134. url: "https://request.dcloud.net.cn/api/http/header/setCookie",
  135. method: "GET",
  136. timeout: 6000,
  137. sslVerify: false,
  138. withCredentials: false,
  139. firstIpv4: false,
  140. success: () => {
  141. this.jest_cookie_upload(true)
  142. },
  143. fail: () => {
  144. this.jest_result = false;
  145. },
  146. });
  147. },
  148. jest_delete_cookie() {
  149. uni.request({
  150. url: "https://request.dcloud.net.cn/api/http/header/deleteCookie",
  151. method: "GET",
  152. timeout: 6000,
  153. sslVerify: false,
  154. withCredentials: false,
  155. firstIpv4: false,
  156. success: () => {
  157. this.jest_cookie_upload(false)
  158. },
  159. fail: () => {
  160. this.jest_result = false;
  161. },
  162. });
  163. },
  164. jest_cookie_upload(needCookie : boolean) {
  165. const imageSrc = "/static/uni.png";
  166. uni.uploadFile({
  167. url: 'https://request.dcloud.net.cn/api/http/header/upload',
  168. filePath: imageSrc,
  169. name: 'file',
  170. success: (res : UploadFileSuccess) => {
  171. const data = JSON.parseObject(res.data)
  172. const errCode = data?.getNumber("errCode")
  173. if (errCode != null && errCode == 1000) {
  174. this.jest_result = needCookie ? false : true;
  175. } else {
  176. this.jest_result = needCookie ? true : false;
  177. }
  178. },
  179. fail: () => {
  180. this.jest_result = false;
  181. },
  182. })
  183. },
  184. jest_files_upload() {
  185. const imageSrc = "/static/uni.png";
  186. uni.uploadFile({
  187. url: 'https://unidemo.dcloud.net.cn/upload',
  188. files: [
  189. {
  190. name: "file1",
  191. uri: imageSrc
  192. } as UploadFileOptionFiles,
  193. {
  194. name: "file2",
  195. uri: imageSrc
  196. } as UploadFileOptionFiles
  197. ],
  198. success: (res : UploadFileSuccess) => {
  199. if (res.statusCode == 200) {
  200. this.jest_result = true;
  201. }
  202. },
  203. fail: () => {
  204. this.jest_result = false;
  205. },
  206. })
  207. },
  208. jest_uts_module_invoked() {
  209. // #ifdef APP-ANDROID || APP-IOS || APP-HARMONY
  210. testInovkeUploadFile({
  211. success: (res : any) => {
  212. this.jest_result = true
  213. },
  214. fail: (err : any) => {
  215. this.jest_result = false
  216. }
  217. } as CommonOptions)
  218. // #endif
  219. }
  220. }
  221. }
  222. </script>
  223. <style>
  224. .image {
  225. width: 100%;
  226. }
  227. .demo {
  228. background: #fff;
  229. padding: 25px;
  230. justify-content: center;
  231. align-items: center;
  232. }
  233. .uni-hello-addfile {
  234. text-align: center;
  235. background: #fff;
  236. padding: 25px;
  237. margin-top: 10px;
  238. font-size: 19px;
  239. color: #808080;
  240. }
  241. </style>