vue 自定义上传图片

vue自定义上传图片

1.用elementUI 里的el-upload上传

   <div class="businesster">
        <el-form-item label="封面图片" prop="headImg">
          <div class="businesster_ying">
            <el-upload
              class="avatar-uploader"
              action="#"
              :show-file-list="false"
              :on-preview="handlePreview"
              :before-upload="beforeAvatarUpload"
              :http-request="uploadFile"
            >
                <el-button class="f_picture">点击上传</el-button>
              <img
                v-if="imageUrl"
                :src="imageUrl"
                class="avatar"
                style="width: 200px"
              />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </div>
        </el-form-item>
        <div slot="tip" class="el-upload__tip">
          <p>
            推荐尺寸750*272;支持.jpg, .jpeg, .bmp, .gif,
            .png类型文件,5M以内
          </p>
        </div>
      </div>

2.:http-request="uploadFile"用自定义上传

js

    data() {
    return {
    	 base64img:"",
    	 imageUrl: require("../../assets/images/andy.png"),
    	 //默认给一张图片
    		}
    }
		  methods: {
	    uploadFile(params){
	      var _this = this
	      console.log("uploadFile",params)
	       if (params.file.name) {
	        var reader = new FileReader();
	        //通过文件流将文件转换成Base64字符串
	        reader.readAsDataURL(params.file);
	        reader.onloadend = function () {
	          // 把转换过得base赋值给全局变量
	          // 这里reader.result就是转换好的bsae64格式图片
	          _this.base64img = reader.result;
	          // console.log(this.base64img);
	          //  this.imageUrl  = URL.createObjectURL(params.file)
	           _this.imageUrl  = _this.base64img,
	           console.log(_this.imageUrl)
	        };
	      }
	    }, 
	      beforeAvatarUpload(file) {
  const isJPG = file.type === "image/jpeg";
  const isPNG = file.type === "image/png";
  const isWebp = file.type === "image/webp";
  const isGif = file.type === "image/gif";
  const isBmp = file.type === "image/bmp";
  const isLt5M = file.size / 1024 / 1024 < 5;

  if (!isJPG && !isPNG && !isWebp && !isGif && !isBmp) {
    this.$message.error(
      "上传封面图片只能是 JPG、png、GIF、JPEG、BMP、webp 格式!"
    );
  }
  if (!isLt5M) {
    this.$message.error("上封面图片大小不能超过 5MB!");
  }
  return (isJPG || isPNG || isWebp || isGif || isBmp) && isLt5M;
},
	  }

3.要注意的是uploadFile 方法没起到作用 是this指向的问题

在这里插入图片描述

补充一下:elementUI里的上传图片显示问题

   //自定义上传方法
   http_request_two(params,fileList){
      console.log(params,"修改营业执照");
      // 需要用URL.createObjectURL()方法转一下file,页面上图片才能加载出来
        this.imageUrl = URL.createObjectURL(params.file)
    },
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于vue-quill-editor的自定义上传图片,你可以按照以下步骤进行操作: 1. 首先,你需要在你的Vue项目中安装vue-quill-editor依赖包。可以使用npm或者yarn命令来安装: ```bash npm install vue-quill-editor # 或者 yarn add vue-quill-editor ``` 2. 在你需要使用vue-quill-editor的组件中引入依赖: ```vue <template> <div> <quill-editor v-model="content" :options="editorOptions" @image-added="handleImageAdded" ></quill-editor> </div> </template> <script> import { quillEditor } from 'vue-quill-editor' export default { components: { quillEditor }, data() { return { content: '', editorOptions: { // 这里可以配置其他选项 } } }, methods: { handleImageAdded(file) { // 自定义处理上传图片的逻辑 // 这里可以使用AJAX或其他方式将图片上传到服务器,然后将返回的图片地址插入到编辑器中 } } } </script> ``` 在上述代码中,我们通过`@image-added`事件监听图片添加的事件,并触发`handleImageAdded`方法来处理上传图片的逻辑。 3. 实现`handleImageAdded`方法,根据你的需求自定义上传图片的逻辑。你可以使用AJAX或其他方式将图片上传到服务器,并获取返回的图片地址。然后,你可以使用Quill提供的API将图片插入到编辑器中。下面是一个示例: ```javascript methods: { handleImageAdded(file) { const formData = new FormData() formData.append('image', file) // 发送AJAX请求将图片上传到服务器 axios.post('/upload', formData) .then(response => { const imageUrl = response.data.imageUrl // 将图片地址插入到编辑器中 const range = this.$refs.editor.quill.getSelection() this.$refs.editor.quill.insertEmbed(range.index, 'image', imageUrl) }) .catch(error => { console.error('上传图片失败', error) }) } } ``` 在上述代码中,我们通过axios库发送了一个POST请求将图片上传到服务器,并获取返回的图片地址。然后,我们使用Quill提供的`insertEmbed`方法将图片地址插入到编辑器中。 请注意,这只是一个示例,具体的上传图片逻辑可能因你的项目需求而有所不同。你需要根据自己的实际情况进行相应的修改。 希望以上信息能对你有所帮助!如果你还有其他问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值