当前位置:   article > 正文

uniapp 使用uview2.0 u-upload组件上传图片文件_u-upload上传文件

u-upload上传文件

uview2.0文档

uni.uploadFile(OBJECT) | uni-app官网

上传图片不能使用uni.request因为data参数不支持fromData数据格式

  1. <template>
  2. <u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="4" width="180rpx" height="180rpx">
  3. <div class="addPic flex justify-center align-center"><u-icon name="plus" :bold="true" color="#172B4D" size="56rpx"></u-icon></div>
  4. </u-upload>
  5. </template>
  6. <script>
  7. export default {
  8. data() {
  9. return {
  10. fileList1: [],
  11. }
  12. },
  13. methods:{
  14. // 删除图片
  15. deletePic(event) {
  16. console.log(event);
  17. this[`fileList${event.name}`].splice(event.index, 1)
  18. },
  19. // 新增图片
  20. async afterRead(event) {
  21. // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
  22. let lists = [].concat(event.file)
  23. let fileListLen = this[`fileList${event.name}`].length
  24. lists.map((item) => {
  25. this[`fileList${event.name}`].push({
  26. ...item,
  27. status: 'uploading',
  28. message: '上传中'
  29. })
  30. })
  31. for (let i = 0; i < lists.length; i++) {
  32. const result = await this.uploadFilePromise(lists[i].url)
  33. let item = this[`fileList${event.name}`][fileListLen]
  34. this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
  35. status: 'success',
  36. message: '',
  37. url: result
  38. }))
  39. fileListLen++
  40. }
  41. },
  42. uploadFilePromise(url) {
  43. console.log("url", url, typeof(url));
  44. return new Promise((resolve, reject) => {
  45. let a = uni.uploadFile({
  46. url: `${config.baseUrl}/common/upload/uploadPicFile`, // 仅为示例,非真实的接口地址
  47. filePath: url,
  48. name: 'multipartFiles',//这个字段很重要,后端根据这个name获取二进制文件
  49. formData: {
  50. type: 11
  51. },
  52. success: (res) => {
  53. console.log("图片接口res", res);
  54. resolve(res.data.data)
  55. }
  56. });
  57. })
  58. },
  59. }
  60. }
  61. </script>

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

闽ICP备14008679号