• 拖拽文件上传


    <html>
    <head>
    <meta charset="UTF-8">
    <meta name="viewport"
    content="width=device-width, user-scalable=no,
        initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./jquery.min.js"></script>
    <style>
    .dropzone {
    border: 2px dashed #0087F7;
    border-radius: 5px;
    background: white;
    display: flex;
    flex-wrap: wrap;
    }
    .file-item {
    100px;
    margin: 20px;
    height: 150px;
    overflow: hidden;
    border: 1px solid #666;
    text-align: center;
    position: relative;
    }

    .file-item button {
    100%;
    height: 20px;
    position: absolute;
    bottom: 0;
    left: 0;
    background: none;
    border: none;
    background-color: #ff4338;
    color: #fff;
    border-top: 1px solid #333;
    }

    .file-item img {
    100px;
    height: 100px;
    object-fit: cover;
    }

    </style>
    </head>

    <body>

    <div id="factory_file" class="tab-pane">
    <div class="tips" style="color:#ff0000">限制10个文件</div>
    <div id="div" class="dropzone" ondrop="drop(event)"
        ondragover="allowDrop(event)" ondragleave="removeDrop(event)"
    style="min-height: 260px;">
    </div>
    <div id="save_files"></div>
    </div>

    </body>
    <script>
      //定义要上传的问价数组
    var uploadFiles = new Array();
    //文件移入
    function allowDrop(event) {
    event.preventDefault(); //阻止浏览器默认事件
    }
    //文件移除
    function removeDrop(event){
    event.preventDefault();
    }
    //当鼠标松开文件
    function drop(event) {
    event.preventDefault();

    var files = event.dataTransfer.files
    if(files.length==0){ //没有文件返回false
    return false
    }
    console.log(files);

    for(var i=0; i<files.length; i++) {
    let file = files[i]
    let type = ''
    // 是图片
    if(file.type.indexOf('image/') !== -1) {
    type = 'image'
    uploadFiles.push({
    type, type,
    imgsrc: window.URL.createObjectURL(file),
    name: file.name,
    file:file,
    })
    } else {
    type = 'pdf'
    uploadFiles.push({
    type: type,
    imgsrc: '',
    name: file.name,
    file:file,
    })
    }
    }

    renderFileList(uploadFiles);


    }

    function renderFileList(list) {
    let html = ''
    for(var i=0; i<list.length; i++) {
    var item = list[i]
    if(item.type === 'image') {
    html += `<div class="file-item"><img src="${item.imgsrc}" />
          <p>${item.name}</p><button onclick="del(${i})">删除</button></div>`
    } else {
    html += `<div class="file-item">PDF<p>${item.name}</p>
              <button onclick="del(${i})">删除</button></div>`
    }
    }
    document.querySelector('#div').innerHTML = html;
    }

    function del(index) {
    uploadFiles.splice(index, 1);
    renderFileList(uploadFiles);
    }

    $('#sub_btn').click(function () {
    var formdata = new FormData(document.querySelector("form"));
    if(uploadFiles.length > 10){
    alert('超出上传文件数量');
    return false;
    }
    for(var j=0; j< uploadFiles.length; j++) {
    formdata.append('files'+j,uploadFiles[j].file)
    }

    $.ajax({
    type: "POST",
    url: "路由",
    data: formdata,
    contentType: false,
    processData: false,
    dataType: 'json',
    success: function (data) {
    alert(data.msg)
    if (data.code == 0) {
    setTimeout(function () {
    window.location.href = "路由";
    }, 1000)
    }
    }
    });
    });
    </script>
    </html>
  • 相关阅读:
    redis 秒杀设计,常用命令
    wamp下配置多域名和访问路径的方法
    git commit 因执行yarn , npm,报错推送不了
    taro bug--小程序报错Error: 未找到入口 sitemap.json 文件,或者文件读取失败,请检查后重新编译。
    vscode引用相对路径,scss时路径报错问题解决
    space-between与space-around的区别
    Android可设置任意高度的TextView,如:设置0.5px设置0.1px等等
    Android可以打开微信支付,但是没法调起小程序支付
    dask
    Joblib-lightweight piplining tool
  • 原文地址:https://www.cnblogs.com/hua-nuo/p/13527298.html
Copyright © 2020-2023  润新知