vue.js

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript类库 > vue.js > vue-upload上传图片

vue-upload上传图片详细使用方法

作者:-風过无痕

这篇文章主要介绍了使用vue-upload上传图片的详细使用说明,文中有相关的代码示例供大家参考,感兴趣的小伙伴一起跟着小编来学习吧

前言

代码实现

1.在添加表单中使用upload组件(饿了吗)

<el-form-item label="维保打印记录:" prop="fileList">
            <!-- <el-input v-model="form.mcRecord" style="width: 350px"></el-input> -->
            <!-- list-type="picture" 上传图片的样式 -->
            <el-upload
              class="upload-demo"
              :action="upload.url"
              :on-preview="handlePreview"
              :headers="upload.headers"
              :on-remove="handleRemove"
              :on-success="handleFileSuccess"
              :file-list="fileList"
              list-type="picture"
              :limit="10"
              :on-exceed="handleExceed"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">
                只能上传jpg/png文件,且不超过500kb
              </div>
            </el-upload>
</el-form-item>

2.在data中设置参数

2.1fileList是上传成功图片存储地方,是一个数组对象,我是直接转换成数组字符串存在后端。

2.2url是当前连接的后端地址加上api地址

2.3headers是upload属性,添加api请求头中的token和tenant-id(用户id)来验证身份。

2.4getToken,getTenantId,是在utils中的方法,获取token和用户id的。

// 上传图片成功之后存储地方
      fileList: [],
      // 图片上传地址,和请求头数据
      upload: {
        // 设置上传的请求头部
        headers: { token: getToken(), "tenant-id": getTenantId() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/media/upload/coursefile",
      },

3.methods中的方法

为了方便查看,在点击已经上传成功文件时,会动态的使用js在document创建一个img来展示图片,方便查看。

// 文件上传成功钩子
    handleFileSuccess(response, file, fileList) {
      console.log("response", response);
      console.log("file", file);
      console.log("fileList", fileList);
      let x = {};
      x.name = response.filename;
      x.url = response.url;
      x.id = response.id;
      console.log("上传图片", x);
      this.fileList.push(x);
    },
    // 点击已上传文件右上角删除钩子
    handleRemove(file, fileList) {
      // console.log("id", file.id);
      console.log("删除之后", fileList);
      // const x = this.fileList.findIndex((v) => v.id == file.id);
      // console.log("删除下标", x);
      // this.fileList.splice(x, 1);
      this.fileList = fileList;
      console.log("处理过数据", this.fileList);
    },
    // 文件上传数量超过设置数量
    handleExceed(files, fileList) {
      this.$message.warning(`最多只能选择10张图片${fileList.length} 个文件`);
    },
    // 点击文件列表中已上传的文件时的钩子
    handlePreview(file) {
      console.log(file);
      const image = new Image();
      image.src = file.url;
      image.onload = () => {
        // 创建弹出层
        console.log("执行了");
        const previewContainer = document.createElement("div");
        previewContainer.style.position = "fixed";
        previewContainer.style.top = 0;
        previewContainer.style.bottom = 0;
        previewContainer.style.left = 0;
        previewContainer.style.right = 0;
        previewContainer.style.zIndex = 99999;
        previewContainer.style.backgroundColor = "rgba(0,0,0,0.8)";
        previewContainer.style.display = "flex";
        previewContainer.style.justifyContent = "center";
        previewContainer.style.alignItems = "center";
        document.body.appendChild(previewContainer);
        // 在弹出层中添加图片
        const previewImage = document.createElement("img");
        previewImage.src = file.url;
        previewImage.style.maxWidth = "80%";
        previewImage.style.maxHeight = "80%";
        previewContainer.appendChild(previewImage);
        // 点击弹出层,关闭预览
        previewContainer.addEventListener("click", () => {
          document.body.removeChild(previewContainer);
        });
      };
    },

注意:

直接复制需要根据实际情况更改url(api接口路径),headers中请求头携带的参数(以request.js文件为准)。

总结:

经过这一趟流程下来相信你也对 vue-upload上传图片详细使用(文档服务器) 有了初步的深刻印象,但在实际开发中我 们遇到的情况肯定是不一样的,所以我们要理解它的原理,万变不离其宗。加油,打工人!

到此这篇关于vue-upload上传图片详细使用方法的文章就介绍到这了,更多相关vue-upload上传图片内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文