uView1.0的Upload组件上传图片

<template>

<u-upload

     ref="uUpload"

    :file-list="fileList"

    accept="image/jpeg,image/png"    //允许选择图片文件

     :sizeType="sizeType"

     :max-size="2 * 1024 * 1024"    //限制上传的图片文件最大为 2M

     @oversize="oversize"

    @on-remove="deleteImgs"    //移除

    :auto-upload="false"    //禁止自动上传选择的图片文

    max-count="9"     //限制最多只能选择几个图片文件       

   :show-progress="false"     //不显示文件上传进度条

   multiple    //多图
   :previewFullImage="true"    //预览图片时显示完整的原图

   @on-choose-complete="onChooseComplete">
</u-upload>
</template>
<script>
  export default {
    data() {
      return {
         fileList: [], //文件列表
      }
    },
    methods: {
      // 文件超出大小限制
      oversize() {
          uni.showToast({
            title: "图片最大不能超过2M",
            icon: 'none'
          })
      },

    onChooseComplete(event) {
      this.multipleUpload(this.$refs.uUpload.lists, 2)
    },
    // 多张图片上传
    multipleUpload(event, type) {
      let num = 9;
      event.map(item => {
        if (num === event.length) {
          uni.showToast({
            title: '最多上传9张图片',
            icon: 'none'
          })
          return
        }
        num += 1
        this.uploadDo(item, type)
      })
    },
    uploadDo(event, type) {
      uni.uploadFile({
        url: "http://192.168.2.105:18100/file/uploadFile",//仅为示例,非真实的接口地址
        filePath: event.file.path,
        name: "file",
        header: {
          Authorization: `Bearer  +uni.getStorageSync("token")`,
        },
        success: (res) => {
          let resp = JSON.parse(res.data);
          this.form.voucherPictureUrls.push(resp.data);
        },
      });
    },
    //删除图片
    deleteImgs(index, lists, name) {

    },
  }
</script>

uploadDo(event, type) {

   console.log(event, "event");        //上传图片事件
      uni.uploadFile({
        url: "http://192.168.2.105:18100/file/uploadFile",//仅为示例,非真实的接口地址
        filePath: event.file.path,
        name: "file",
        header: {
          Authorization: `Bearer  +uni.getStorageSync("token")`, //要添加header否则接口不能调用
        },
        success: (res) => {

         console.log(res, "res");

          //如果上传成功打印下res,看有没有调用接口以及图片上传回显
          let resp = JSON.parse(res.data);
          this.form.voucherPictureUrls.push(resp.data);
        },
      });
    },

UView UI库中的`upload`组件主要用于文件上传功能,支持多种类型的文件上传,包括常见的图片、文件等。如果想要上传Word文档(.doc或.docx格式),通常需要确保你的服务器端支持接收并处理这种格式的文件,因为HTML5的`<input type="file">`默认并不支持直接上传特定格式的文档。 在UView中,你可以按照以下步骤操作: 1. **设置上传组件**: 首先,在你的Vue模板中引入UView的`u-upload`组件,并配置一些基本属性,如`action`用于指定文件上传的URL,`before-upload`可以用于添加自定义的上传前验证逻辑。 ```html <u-upload ref="upload" action="/api/upload" :before-upload="beforeUpload" accept=".doc,.docx" > <i slot="trigger" class="u-icon u-icon-upload"></i> </u-upload> ``` 这里的`accept`属性指定了允许上传的文件类型,`.doc,.docx`表示接受Word文档。 2. **文件预处理**: 使用`beforeUpload`钩子函数来检查文件类型、大小等,确保上传的是Word文档。 ```javascript methods: { beforeUpload(file) { if (!/(\.doc|\.docx)$/.test(file.name)) { this.$message.error('只能上传Word文档'); return false; } // 如果需要限制文件大小,这里也可以做检查 const maxSize = 10 * 1024 * 1024; // 10MB if (file.size > maxSize) { this.$message.error('文件大小超过10MB'); return false; } return true; }, } ``` 3. **接收服务端响应**: 服务器接收到文件后会返回响应,一般会有状态码和结果信息。根据服务器的反馈,可以更新UI或者处理后续业务逻辑。 注意:这个过程需要配合服务器端的支持才能成功,例如PHP Laravel、Node.js Express等后端框架都应能够处理Word文档的上传和存储。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值