vue3.2图片上传Demo搭建 2 图片模拟 模拟数据

pages/MyFiles.vue

<template>
    <div class="container py-3">
        <ActionsBar />
        <div class="d-flex justify-content-between  align-items-center  py-2">
            <h6 class="text-muted mb-0">文件</h6>
            <button class="rounded-button">
                <icon-arrow-up />
            </button>
        </div>

        <div class="row">
            <!-- 图片效果-->
            <div class="col-md-3" v-for="(item, index) in 6" :key="index">
                <div class="card mb-4">
                    <img class="file-thumb" v-if="true" src="https://picsum.photos/id/1015/400/160" />
                    <div v-else class="card-body text-center py-5">
                        <icon-type-common height="4em" width="4em" />
                    </div>
                    <div class="card-footer">
                        <div class="d-flex align-items-center">
                            <icon-type-commion />
                            <span class="file-name">File 8.png</span>
                        </div>
                    </div>
                </div>
            </div>
  <!-- 图片效果-->

        </div>



    </div>

</template>
<script setup>
import ActionsBar from '../components/ActionsBar.vue'

</script>

<style>
</style>

安装  npm i -D json-server multer concurrently    -D//仅在开发模式下使用的

 json-server@0.17.0  用来模拟数据
+ multer@1.4.4   //上传图片的插件。
+ concurrently@7.2.0  //实现前后端连载

根目录创建 数据模拟 server/db.json  

{
"files":[
{
"id":1,
"name":"1.jpg",
"url":"地址",
 "path": "",
"mimeType":"image/jpeg"

}



]


}

新建文件夹 server/uploads

启动数据 开启新终端 json-server server/db.json --watch --port=3031

新建index.js

const jsonServer = require("json-server");

const path = require("path");

const cors = require("cors");

var multer = require("multer");
// const { off } = require("process");

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        //文件储存地址 uploads
        cb(null, path.join(__dirname, "./uploads"));
    },
    filename: function (req, file, cb) {
        const fileName = file.originalname.toLowerCase().split("").join("-");
        cb(null, fileName);
    },


});

var upload = multer({
    storage,
    fileFilter: validateFileType,
}).single("file");

const server = jsonServer.create();

const router = jsonServer.router(path.join(__dirname, "db.json"));

const middlewares = jsonServer.defaults();

const port = 3031;

server.options("*", cors());
server.post("/files", upload, function (req, res, next) {
    const host = "http://localhost:" + port;
    req.body.name = req.file.originalname;
    req.body.mimeType = req.file.mimetype;
    req.body.url = host + "/uploads/" + req.file.filename;
    req.body.path = req.file.path;
    req.body.createdAt = Date.now();
    next();
});

server.delete("/files/:id", (req, res, next) => {
    const fs = require("fs");
    const file = router.db.get("files")
        .findKey({ id: parseInt(req.params.id) })
        .value();
    try {
        fs.unlinkSync(file.path);

    } catch (err) {
        console.error(err);

    }
    next();
});

server.use(middlewares);
server.use(router);
server.listen(
    port, () => {
        console.log("JSON Server is  running at port", port);
    }
);

function validateFileType(req, file, cb) {
    if (!file.originalname.match(/\.(jpg|jpeg|png|gif|doc|docx|pdf|xls|xlsx)$/)) {
        return cb(new Error("您尝试上传的文件不合法"));
    }
    cb(null, true);
}

启动 node server/index.js

同时启动前端和后端 配置  package.json

{
  "name": "vue-drive",
  "private": true,
  "version": "0.0.0",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview",
    "api":"node server/index",
    "start": "concurrently \"npm  run api\"\"npm run dev\""  
  },
  "dependencies": {
    "vue": "^3.2.25"
  },
  "devDependencies": {
    "@vitejs/plugin-vue": "^2.3.3",
    "concurrently": "^7.2.0",
    "json-server": "^0.17.0",
    "multer": "^1.4.4",
    "vite": "^2.9.9"
  }
}

npm run start

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值