转 vue批量上传图片

HTML代码: 

 <div id="app">
    <div class="hello">
      <div class="upload">
        <div class="upload_warp_left" @click="fileClick">
          <button class="btn btn-success">上传图片</button>
        </div>
        <div class="upload_warp_text">
          选中{{imgList.length}}张文件,共{{bytesToSize(this.size)}}; 最多上传<span class="text-primary"> 6</span> 张图片!
        </div>
        <div class="upload_warp" style="border: 1px solid white;">
          <div class="upload_warp_img" v-show="imgList.length!=0">
            <div class="upload_warp_img_div" v-for="(item,index) of imgList">
              <div class="upload_warp_img_div_top">
                <div class="upload_warp_img_div_text">
                  {{item.file.name}}
                </div>
                <img src="./del.png" class="upload_warp_img_div_del" @click="fileDel(index)">
              </div>
              <img :src="item.file.src">
            </div>
          </div>
        </div>


        <input @change="fileChange($event)" type="file" id="upload_file" multiple style="display: none"/>

      </div>
    </div>
  </div>

CSS 代码:


  .upload_warp_img_div_del {
    position: absolute;
    top: 6px;
    width: 16px;
    right: 4px;
  }

  .upload_warp_img_div_top {
    position: absolute;
    top: 0;
    width: 100%;
    height: 30px;
    background-color: rgba(0, 0, 0, 0.4);
    line-height: 30px;
    text-align: left;
    color: #fff;
    font-size: 12px;
    text-indent: 4px;
  }

  .upload_warp_img_div_text {
    white-space: nowrap;
    width: 80%;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .upload_warp_img_div img {
    max-width: 100%;
    max-height: 100%;
    vertical-align: middle;
  }

  .upload_warp_img_div {
    position: relative;
    height: 100px;
    width: 120px;
    border: 1px solid #ccc;
    margin: 0px 5px 5px 0px;
    float: left;
    line-height: 100px;
    display: table-cell;
    text-align: center;
    background-color: #eee;
    cursor: pointer;
  }

  .upload_warp_img {
    border-top: 1px solid #D2D2D2;
    padding: 5px 0 0 5px;
    overflow: hidden
  }

  .upload_warp_text {
    text-align: left;
    margin-bottom: 5px;
    padding-top: 5px;
    text-indent: 14px;
    border-top: 1px solid #ccc;
    font-size: 14px;
  }

  .upload_warp_right {
    float: left;
    width: 57%;
    margin-left: 2%;
    height: 100%;
    border: 1px dashed #999;
    border-radius: 4px;
    line-height: 130px;
    color: #999;
  }

  .upload_warp_left button {

    margin: 8px 5px 0px 5px;
    cursor: pointer;

  }

  .upload_warp_left {
    float: left;

  }

  .upload_warp {
    margin: 5px;

  }

  .upload {
    border-left: 1px solid #ccc;
    border-right: 1px solid #ccc;
    background-color: #fff;
    width: 770px;
    box-shadow: 0px 1px 0px #ccc;
    border-radius: 4px;
  }

  .hello {
    width: 770px;
    text-align: center;
  }

 JS 代码:

var app = new Vue({
        el: '#app',
        data () {
            return {
                imgList: [],
                size: 0
            }
        },
        methods: {
            fileClick(){
                document.getElementById('upload_file').click()
            },
            fileChange(el){
                if (!el.target.files[0].size) return;
                this.fileList(el.target.files);
                el.target.value = ''
            },
            fileList(files){
                for (let i = 0; i < files.length; i++) {
                    this.fileAdd(files[i]);
                }
            },
            fileAdd(file){
                this.size = this.size + file.size;//总大小
                let reader = new FileReader();
                reader.vue = this;
                reader.readAsDataURL(file);
                reader.onload = function () {
                    file.src = this.result;
                    this.vue.imgList.push({
                        file
                    });
                }
            },
            fileDel(index){
                this.size = this.size - this.imgList[index].file.size;//总大小
                this.imgList.splice(index, 1);
            },
            bytesToSize(bytes){
                if (bytes === 0) return '0 B';
                let k = 1000, // or 1024
                    sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
                    i = Math.floor(Math.log(bytes) / Math.log(k));
                return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i];
            },
            dragenter(el){
                el.stopPropagation();
                el.preventDefault();
            },
            dragover(el){
                el.stopPropagation();
                el.preventDefault();
            },
            drop(el){
                el.stopPropagation();
                el.preventDefault();
                this.fileList(el.dataTransfer.files);
            }
        }
    })

最多上传6张是我的项目需要加上去的,实际并没有限制,可以无限上传,需要VUE2.2 以上版本支持

  原文允许转载或者本次转载已经获得原文作者授权

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
为了在Vue3中使用wangEditor富文本批量上传图片,你需要按照以下步骤进行操作: 1.首先,你需要安装wangEditor。你可以通过npm安装wangEditor,命令如下: ```shell npm install wangeditor --save ``` 2.在你的Vue组件中引入wangEditor,并在mounted钩子函数中初始化wangEditor。你需要在初始化时设置上传图片的配置,包括上传图片的路径和上传图片的处理函数。以下是一个示例: ```javascript <template> <div ref="editorElem" /> </template> <script> import wangEditor from 'wangeditor' export default { mounted() { const editor = new wangEditor(this.$refs.editorElem) // 配置上传图片的路径和处理函数 editor.customConfig.uploadImgServer = '/upload' editor.customConfig.uploadImgHooks = { before: function(xhr, editor, files) { // 在这里可以对上传图片进行处理 }, success: function(xhr, editor, result) { // 图片上传成功后的处理函数 }, fail: function(xhr, editor, result) { // 图片上传失败后的处理函数 }, error: function(xhr, editor) { // 图片上传出错的处理函数 }, timeout: function(xhr, editor) { // 图片上传超时的处理函数 }, customInsert: function(insertImg, result, editor) { // 在这里可以对插入的图片进行处理 insertImg(result.data) } } editor.create() } } </script> ``` 在上面的代码中,你需要将`/upload`替换为你的图片上传路径。在`uploadImgHooks`中,你可以设置上传图片的处理函数,包括上传前的处理函数、上传成功后的处理函数、上传失败后的处理函数、上传出错的处理函数、上传超时的处理函数和插入图片后的处理函数。在`customInsert`函数中,你可以对插入的图片进行处理。 3.在你的服务器端设置图片上传的处理函数。在上传图片的处理函数中,你需要将上传图片保存到服务器上,并返回一个JSON格式的数据,包括图片的URL和图片的宽度和高度。以下是一个示例: ```python import os from flask import Flask, request, jsonify from werkzeug.utils import secure_filename app = Flask(__name__) @app.route('/upload', methods=['POST']) def upload(): file = request.files['file'] filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) url = 'http://example.com/uploads/' + filename return jsonify({ 'errno': 0, 'data': [url], }) ``` 在上面的代码中,你需要将`http://example.com/uploads/`替换为你的图片上传路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值