美文网首页
node图片服务器

node图片服务器

作者: 猪猪9527 | 来源:发表于2018-01-22 14:19 被阅读7次

今天写了个图片上传的小demo并部署到腾讯云【Ubuntu】

准备工作:

腾讯云 开放3000 端口

Mac OS

apt-get update  
apt-get install -y python-software-properties software-properties-common  
apt-get install nodejs

node 后台运行

sudo npm install forever -g   #安装
forever start app.js          #启动
forever stop app.js           #关闭
forever start -l forever.log -o out.log -e err.log app.js   #输出日志和错误

查看日志

tail -f out.log

项目目录结构

full.png

app.js

"use strict";
exports.__esModule = true;
var express = require("express");
var multer = require("multer");
var storage = multer.diskStorage({
    destination: function(req, file, cb) {
        cb(null, './public');
    },
    filename: function(req, file, cb) {
        cb(null, `${Date.now()}-${file.originalname}`)
    }
})
var upload = multer({ storage: storage });
var app = express();
app.use("/",express.static(__dirname + "/public"));

app.post('/uploadimg', upload.array('imgfile', 40), function(req, res, next) {
    var files = req.files
    console.log(files)
    if (!files[0]) {
        res.send('error');
    } else {
        res.send('success');
    }
    console.log(files);
})

var server = app.listen(9999, 'localhost', function() {
    console.log('server is running at port 9999...');
});

index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <style>
        .thumbPic {
            width: 100px;
            height: 100px;
            margin: 1px;
            border: 1px solid #ccc;
            padding: 1px;
            min-width: 100px;
            overflow: hidden;
        }
        
        .progress {
            width: 100vw;
            height: 100vh;
            display: flex;
            position: fixed;
            top: 0;
            left: 0;
            display: none;
            justify-content: center;
            align-items: center;
            z-index: 10;
            background-color: rgba(1, 1, 1, 0.3);
        }
        
        #progress {
            background: green;
        }
        
        .thumbPic img {
            width: 100%;
            height: 100%;
        }
        
        .preview {
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: flex-start;
            padding: 2px;
        }
        
        input[type="button"] {
            width: 5em;
            height: 2em;
            position: relative;
            top: -9px;
            display: inline-block;
            color: #eee;
            background-color: seagreen;
            outline: none;
            border: none;
            cursor: pointer;
            letter-spacing: 2px;
            font-family: '黑体';
        }
        
        #file {
            z-index: 10;
            width: 30px;
            opacity: 0;
            position: absolute;
            left: 0px;
            cursor: pointer;
            top: 0px;
        }
        
        #upload {
            margin-left: 100px;
        }
        
        .selectImg {
            display: inline-block;
            width: 30px;
            cursor: pointer;
            height: 27px;
            background-position: -206px -87px;
            background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAvgAAAB4CAYAAACHDYYSAAAhFElEQVR42u2dC5AcVbmANwoanpfHRRQBIyhEBESw5HmBiAbwQsIj4ZXszizBWypqAYJSKlWrVQqphAT2PUs2aNSosQqkEKooX6ggqCivKgQRQSCKUtESVN7J/f/Znt0zvT09s9PndJ/u+b6qrt2d7ek+ffo8vj59Hl1dAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAN5QqVS2mBsxkiz+wvHY6O9W9wcAoJwtZrlIeQ8ACD6CDwCA4HNd2TE6Ovpko0osYntO9n9Ytu/J76f19fVtlXFk39hiuJ8ly0ESAXS1uS5o0g6flAlvkPLhKvnun3XT3/WzvBTUtj4HaOcB09fj+iZGrvKd6/w8MjLyXjn+sGx3Stn4uPx8QX4+LT9vl59rZDs2j/Vb0ni0fV5X9Zzt87ryg0mGh4ePkANdLtsToYM/JNt5kuAWSaI8S36/TH6/wdxH/v69/Nw7w8JyXwlbt/y8NSJyVC4ukes7rr+/f0+qFkDw3YcvkPstoXLiKgQfAMHvVMHXxlA57oaIe6Te9aiUka8Zn/1AtoMQfATf2o2/5pprdpcD/tuolH/UoAL/kGyvmAl0YGBgDw8Kzt8YYXpJxP+tVCfgWwuOrfP4KpZBy/20h+2sHzzyJvjNjttuReP6/q9evXonqR/W5zX/a9j1Gjq9fEHwrR93ZSgvfnNwcHDX2v9XrFixnaS9q+XzzYF//SUPDpNVw0ennddW4XZfM8EPLmYglFhXepDQHjHCfjOqCgg+go/gpyf4cvx5tS6fec7/wTXMQ/ARfItu9ZiRD2+L2e87hsf8Mutu0Ih2sQT/160Ivvzv1FDFcWeW4dYn4VCXgM+i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xLan/dHpqnYkQwee8Xgn+dCFM9ynUnHZRB5S6nu1gStLXT3a3id42VveJkvOk358puiBZlOTrwNlwv+jBnl0bduuplD7mtJCbqeS7kvuojGqj9X4qfD9D8AEQfAQfwS90vkXwfRT88u8MyXqw4X4T02S+ZOz7WKrdcybE6+6G3R/AiKfS/Or6ADpQttJ7X/W14PIFO0Tuq630lfIVEqc3y/ZodUEzHYCbRkuGOQBYH3AGut8en/56b7Au92lm1LSmyWQl23TFwNZ88L5P44ngF7t88fU4ruMRwUe0Cyn42hWhfk7WzSKHn6pbiVMr6uqiPkZ/ba3Eh8/bJ91ENW3Wl2dSf8AAB5Jf/lHXcOmwGWS2o+Q7d1iTewS/+CvZIvgIPoKP4Gd9f7Q782jv0upYuYlplPM3RSaCnwPB15ZQs+U+arXR6uJHuphP+VXjf3+rtvbqYNd0BPCy6sDQht1Iyv+R7afVRZ0AiiqAvhcsvi90RfpzG4++hMfX9JWXLjAIvut8fmlq3WDTKF+SxqPt87rK77bP67wcqvSuan0rf6nagq/9o1ct3iZlcfhyi+EsYamA4CP4pD8EH8FH8L0sTyvl20Ljzm7KdfmC4DMWBAA8rcitha/DuugAQHb5OL+Cf2OoEeRb3B+uCwCo2FjJFsEHoBzMq8DpNN+V3leCBpCXukZLJ3B/uC4AAAAAyDOVnr2rUz7bXCUeAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACgiPw/PzHaNf0IqRAAAAAASUVORK5CYII=)
        }
        
        .wrap {
            position: relative;
        }
        
        .wrap a {}
    </style>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>图片上传
    </title>
</head>

<body>
    <!--<form action="uploadimg" method="POST" enctype="multipart/form-data">
        <input type="file" name="imgfile" multiple>
        <input type="submit" value="提交">
    </form>-->
    <div class="wrap">

        <a href="" class="selectImg" title="上传图片"></a>
        <input type="file" multiple id="file">
        <input type="button" value="上传" id="upload">
        <div class="preview"></div>
        <div class="progress">
            <progress max="100" value="1" item-width="100" id="progress"></progress>
        </div>
    </div>
    <script>
        var aUpload = document.querySelector('.selectImg')
        var button = document.querySelector('#upload')
        var fileinput = document.getElementById('file')
            // aUpload.addEventListener('click', function(e) {

        //     var evt = new MouseEvent('click', {
        //         bubbles: false,
        //         cancelable: true,
        //         view: window
        //     })


        //     return fileinput.dispatchEvent(evt)

        // }, false)
        button.onclick = uploadFile

        let previewDOM = document.getElementsByClassName('preview')[0]
        fileinput.onchange = () => {
            //console.log('dddd')
            var files = fileinput.files
            let imgDOMArray = new Array(files.length)
            let reader = []
            let thumbPic = []
            progressDOM = document.getElementById('progress-img')
            for (let i = 0; i < files.length; i++) {
                reader[i] = new FileReader()
                thumbPic[i] = document.createElement('div')
                imgDOMArray[i] = document.createElement('img')
                imgDOMArray[i].file = files[i]
                thumbPic[i].className = 'thumbPic'
                thumbPic[i].appendChild(imgDOMArray[i])
                previewDOM.appendChild(thumbPic[i])
                reader[i].readAsDataURL(files[i])
                reader[i].onload = (img => {

                    return e => {
                        img.src = e.target.result
                    }
                })(imgDOMArray[i])

            }
        }

        function uploadFile() {
            //  console.log('ddd')
            var xhr = new XMLHttpRequest()
            var formdata = new FormData()

            var files = fileinput.files
            if (!files[0]) {
                alert('请先选择图片,再上传!')
                return
            }

            var progress = document.querySelector('progress')
            for (let i = 0; i < files.length; i++) {
                formdata.append('imgfile', files[i], files[i].name)
            }
            xhr.open('POST', '/uploadimg')
            xhr.onload = () => {
                if (xhr.status === 200 && xhr.responseText === 'success') {
                    previewDOM.innerHTML = ''
                    xhr = null
                    alert('图片上传成功!')
                }
            }
            xhr.send(formdata)
            xhr.upload.onprogress = e => {
                if (e.lengthComputable) {
                    var progressWrap = document.querySelector('.progress')
                    progressWrap.style.display = "flex"
                    var percentComplete = e.loaded / e.total * 100
                    progress.value = percentComplete

                    if (percentComplete >= 100) {
                        progress.value = 0
                        progressWrap.style.display = "none"
                    }
                }
            }

        }
    </script>
</body>

</html>

相关文章

网友评论

      本文标题:node图片服务器

      本文链接:https://www.haomeiwen.com/subject/jvplaxtx.html