input上传--upload

1.HTML

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>上传文件</title>
    <link rel="stylesheet" href="../css/upload.css">
</head>
<body>
    <div class="upload">
        <h3>上传文件</h3>
        <div></div>
        <input type="file">
    </div>
</body>
</html>
<script src="../js/upload.js"></script>

2.CSS

css 复制代码
*{margin: 0;padding: 0;}
ul,li,ol{list-style-type: none;}
button{cursor: pointer;border: none;background: rgba(255, 255, 255, 1);}
.upload{
    width: 170px;
    height: 200px;
    margin: 200px auto;
    border: 1px #ccc solid;
    display: flex;
    flex-direction: column;
    align-items: center;
    position: relative;
}
h3{
    width: 100%;
    height: 30px;
    border-bottom: 1px #ccc solid;
}
        
input{
    width: 170px;
    height: 170px;
    position: absolute;
    opacity: 0;
}
.upload>div{
    width: 170px;
    height: 170px;
    background: url(../image/upload.png) no-repeat center center/30% 30%;
}

3.JS

javascript 复制代码
const formData = new FormData();
const fileField = document.querySelector('input[type="file"]');
fileField.addEventListener('change', function() {
    formData.append("file", fileField.files[0],encodeURIComponent(fileField.files[0].name));


    fetch("http://localhost:8080/api/upload", {
        method: "POST",
        body: formData,
    }).then(response => response.json()).then(res => {
        console.log("res:", res);
    }).catch(err => {
        console.log("err:", err);
    });
});

4.资源

5.运行截图

6.下载链接

在此处下载压缩包

【免费】原生html,js上传文件资源-CSDN文库

7.注意事项

此压缩包 包含前后端简单交互,后端需要用到Node.js,运行口令 nodemon app.js

或者在serve文件夹直接运行run.bat文件,运行成功后打开html即可。注意:run.bat运行成功后不要退出cmd。

相关推荐
IT_陈寒3 分钟前
Vue3性能优化实战:这5个技巧让我的应用加载速度提升70% 🚀
前端·人工智能·后端
南山安6 分钟前
面试必考点: 深入理解CSS盒子模型
javascript·面试
golang学习记6 分钟前
从0死磕全栈之深入理解 Next.js 中的 NextResponse:API 详解与实战示例
前端
木易士心8 分钟前
CSS 中 `data-status` 的使用详解
前端
木易士心8 分钟前
JavaScript 数组的核心操作方法,从基础到高级
前端
TimelessHaze9 分钟前
🧱 一文搞懂盒模型box-sizing:从标准盒到怪异盒的本质区别
前端·css·面试
VOLUN11 分钟前
Vue3 中 watch 第三个参数怎么用?6 大配置属性 + 场景指南
前端·javascript·vue.js
Larcher11 分钟前
100 行代码搞定 AI Logo 生成网站!新手也能吃透的 AIGC 前端实战
前端·javascript
社恐的下水道蟑螂15 分钟前
彻底搞懂 CSS 盒子模型:从 content-box 到 border-box 的实战指南
javascript
Data_Adventure22 分钟前
Java 与 TypeScript 的核心对比
前端·后端