当前位置:   article > 正文

uview自定义样式上传图片_uview上传图片

uview上传图片
  1. <yz-upload :fileList1="fileList1" :maxCount="1">
  2. <template v-slot>
  3. <div style="width: 200rpx; height: 200rpx">自定义内容</div>
  4. </template>
  5. </yz-upload>
  1. <template>
  2. <u-upload
  3. :fileList="fileList1"
  4. @afterRead="afterRead"
  5. @delete="deletePic"
  6. name="1"
  7. multiple
  8. :maxCount="maxCount"
  9. >
  10. <slot></slot>
  11. </u-upload>
  12. </template>
  13. <script>
  14. export default {
  15. props: {
  16. maxCount: { // 最大上传数量
  17. type: Number,
  18. default: 1,
  19. },
  20. fileList1: { // 父组件接受数组
  21. type: Array,
  22. default: () => [],
  23. },
  24. name: { // 标识 name1 父组件接受值 fileList1 name:2 父组件接受值 fileList2 在data中声明即可
  25. type: String,
  26. default: "1",
  27. },
  28. },
  29. data() {
  30. return {};
  31. },
  32. methods: {
  33. // 删除图片
  34. deletePic(event) {
  35. console.log(event.name, "event.name");
  36. this[`fileList${event.name}`].splice(event.index, 1);
  37. },
  38. // 新增图片
  39. async afterRead(event) {
  40. // 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式
  41. let lists = [].concat(event.file);
  42. let fileListLen = this[`fileList${event.name}`].length;
  43. lists.map((item) => {
  44. this[`fileList${event.name}`].push({
  45. ...item,
  46. status: "uploading",
  47. message: "上传中",
  48. });
  49. });
  50. for (let i = 0; i < lists.length; i++) {
  51. const result = await this.uploadFilePromise(lists[i].url);
  52. let item = this[`fileList${event.name}`][fileListLen];
  53. this[`fileList${event.name}`].splice(
  54. fileListLen,
  55. 1,
  56. Object.assign(item, {
  57. status: "success",
  58. message: "",
  59. url: JSON.parse(result).data,
  60. })
  61. );
  62. fileListLen++;
  63. }
  64. },
  65. uploadFilePromise(url) {
  66. return new Promise((resolve, reject) => {
  67. let a = uni.uploadFile({
  68. url: "http://taiwan.yunzanet.com/api/common/upload", // 仅为示例,非真实的接口地址
  69. filePath: url,
  70. name: "file",
  71. formData: {},
  72. header: {
  73. token: uni.getStorageSync("token"),
  74. },
  75. success: (res) => {
  76. setTimeout(() => {
  77. resolve(res.data);
  78. }, 1000);
  79. },
  80. });
  81. });
  82. },
  83. },
  84. };
  85. </script>

打印fileList1 

默认

 

自定义内容 点击即可上传

 

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

闽ICP备14008679号