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。

相关推荐
杨芋爽14 分钟前
管理后台列表页为什么一直转圈?从一次 1+N 请求扇出聊聊前端性能优化
javascript
晴天1624 分钟前
Chrome DevTools 深度调试指南
前端·chrome·chrome devtools
林冠宏_指尖下的幽灵26 分钟前
AI发展下的后编程时代思考
前端·人工智能·后端
_山海1 小时前
Bun入门指南
前端·javascript·后端
想睡懒觉1 小时前
我用 CSS 3D 做了个星空隧道 Loading,没有 Three.js
前端
labixiong1 小时前
transition 写在 CSS 变量上为什么不生效?给变量补张「类型身份证」
前端·css·html
YHL1 小时前
🧠 Agent 记忆进阶:总结压缩与向量检索 —— 从截断到 Milvus 长期记忆
前端·人工智能
YHL1 小时前
🎯 JavaScript 单例模式(Singleton Pattern)—— 从理论到实战
javascript·设计模式
用户921080262861 小时前
前端 Vue 专栏 05:从 Object.defineProperty 到 Proxy,彻底理解 Vue 响应式原理
前端