zoukankan      html  css  js  c++  java
  • 文件上传及图片显示

    客户端:

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            .box {
                 800px;
            }
            
            .box img {
                 100%;
            }
            
            .fbar {
                 800px;
                height: 20px;
                border: 1px solid #ccc;
                border-radius: 5px;
                overflow: hidden;
            }
            
            .bar {
                 0%;
                background-color: blue;
                text-align: center;
                line-height: 20px;
            }
        </style>
    </head>
    
    <body>
        <form id="form">
            <input type="file" name="" id="file">
            <div class="box" id="box">
    
            </div>
            <div class="fbar" id="fbar">
                <div class="bar" id="bar">0%</div>
            </div>
        </form>
    
        <script>
            var form = document.getElementById('form')
            var file = document.getElementById('file')
            var bar = document.getElementById('bar')
            var box = document.getElementById("box")
            file.onchange = function() {
                var formData = new FormData();
                formData.append('attrName', this.files[0])
                var xhr = new XMLHttpRequest();
                xhr.open('post', 'http://localhost:3001/upload');
                xhr.upload.onprogress = function(ev) {
                    var result = ev.loaded / ev.total * 100;
                    result = result.toFixed(2) + '%'
                    bar.innerHTML = result
                    bar.style.width = result
                }
                xhr.send(formData);
                xhr.onload = function() {
                    var response = JSON.parse(xhr.responseText)
                    var img = document.createElement('img')
                    img.src = response.path
                    img.onload = function() {
                        box.appendChild(img)
                    }
                }
            }
        </script>
    
    </body>
    
    </html>
    

      

    服务端

    app.post('/upload', (req, res) => {
        const form = new formidable.IncomingForm()
        form.uploadDir = path.join(__dirname, 'public', 'uploads')
        form.keepExtensions = true
        form.parse(req, (err, fields, files) => {
            res.send({ path: files.attrName.path.split('public')[1] })
        })
    })
    

      

  • 相关阅读:
    方法名的string类型应用(补)
    unity3D里面的点乘和叉乘
    C# 计算时间日期
    iOS设备屏幕分辨率分布
    免证书发布ipa文件真机测试
    unity3D +php +数据库
    windows下mysql5.1忘记root密码解决方法[win7]
    springboot配置多数据源(JdbcTemplate方式)
    【转】Google Chrome中顺时针/逆时针滚动圆的含义
    Redis内存模型(2):存储细节
  • 原文地址:https://www.cnblogs.com/rainbowupdate/p/13055031.html
Copyright © 2011-2022 走看看