当前位置:   article > 正文

uni-app uni-file-picker 上传组件实战应用_uni-file-picker详细使用教学

uni-file-picker详细使用教学

html

  1. <!-- 导入---------------------------------------- -->
  2. <uni-file-picker
  3. limit="1"
  4. :file-extname="fmt"
  5. file-mediatype="all"
  6. :disabled="percent < 100"
  7. :auto-upload="false"
  8. @select="beforeUpload"
  9. >
  10. <button
  11. type="primary"
  12. size="mini"
  13. :disabled="percent < 100">
  14. {{ 100 > percent ? percent + "%" : "导入数据" }}
  15. </button>
  16. </uni-file-picker>

data

  1. // 上传----------------------------------------
  2. headers: {
  3. token: uni.getStorageSync('token'), //获取token(注意仔细看后端接受token的字段名是不是叫做“token”)
  4. },
  5. actionUrl: `${this.$d.API_ROOT_URL}/XXX/XXX`,
  6. fmt: ["xls", "xlsx"],
  7. dur: 100,
  8. percent: 100,
  9. // ----------------------------------------

script 

  1. // 上传文件----------------------------------------------------------------
  2. showFakeLoading() {
  3. this.interval && clearInterval(this.interval);
  4. this.percent = 0;
  5. this.interval = setInterval(() => {
  6. this.percent++;
  7. this.percent >= 99 && clearInterval(this.interval);
  8. }, this.dur);
  9. },
  10. hideFakeLoading() {
  11. this.interval && clearInterval(this.interval);
  12. this.percent = 100;
  13. },
  14. //文件上传之前
  15. beforeUpload(e) {
  16. const filePath = e.tempFilePaths[0];
  17. const file = e.tempFiles[0];
  18. //获取图片临时路径
  19. uni.uploadFile({
  20. url: this.actionUrl, //【必填】图片上传地址
  21. filePath, //【必填】(files和filePath选其一)要上传文件资源的路径。
  22. name: 'file', //【必填】上传名字,注意与后台接收的参数名一致
  23. header: this.headers, //设置请求头
  24. //请求成功,后台返回自己服务器上的图片地址
  25. success: d => {
  26. d = JSON.parse(d.data)
  27. // 上传成功了
  28. if (d.success) {
  29. let response = d;
  30. if (response.data && response.data.key) {
  31. // 下载失败原因的描述文件
  32. this.$d.customer_downloadImportCustomerExcel({
  33. key: response.data.key
  34. }, {
  35. s: (d) => {
  36. this.$g.downloadFile(d, `${file.name}-上传失败原因`, '.xls');
  37. uni.showToast({
  38. icon: `none`,
  39. title: `${file.name}-上传失败,请查看失败原因`
  40. });
  41. setTimeout(() => {
  42. this.initList(true); //刷新列表
  43. }, 1500);
  44. //console.log('上传失败', response, file, fileList);
  45. }
  46. });
  47. } else if (response.success) {
  48. // 上传成功了
  49. uni.showToast({
  50. icon: `none`,
  51. title: `“${file.name}”导入成功`
  52. });
  53. setTimeout(() => {
  54. this.initList(true); //刷新列表
  55. }, 1500);
  56. //console.log('上传成功', response, file, fileList);
  57. } else {
  58. // 其他失败原因
  59. uni.showToast({
  60. icon: `none`,
  61. title: response.msg
  62. });
  63. //console.log('上传失败', response, file, fileList);
  64. }
  65. } else {
  66. uni.showToast({
  67. icon: `none`,
  68. title: d.msg
  69. });
  70. }
  71. }
  72. });
  73. },
  74. // ----------------------------------------

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/183472
推荐阅读
相关标签
  

闽ICP备14008679号