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。

相关推荐
SendTomo2 分钟前
P2P文件传输工具对比:SendTomo vs send.wang
javascript·网络协议·webrtc·html5·p2p
犹豫的果冻布丁1 小时前
从零给 DeepSeek Harness 写一个壁纸皮肤插件(已开源)
前端·后端
漏刻有时1 小时前
数据可视化Three.js 3D 地图实战:单文件原生实现省域区县拉伸建模
前端
会说话的番茄2 小时前
AI 满嘴跑火车怎么办?给它配个"小抄"
前端·aigc
计算机魔术师2 小时前
AI 权力集中辩论实录:从「token 工厂」到「唯一幸存者」
前端
eric-sjq2 小时前
WanlyFrontend 中文声明式前端语言完全指南:让 0.6B 模型写出漂亮网页
前端
Full Stack Developme2 小时前
跨站请求伪造 (CSRF) 是什么 设计及工作原理
前端·okhttp·csrf
濮水大叔2 小时前
CabloyJS 强大之处不仅仅是 IoC,而是全栈资源的寻址体系
typescript·node.js·全栈
SendTomo2 小时前
WebRTC文件互传工具实测对比
javascript·网络·webrtc·html5·p2p
MoSTChillax2 小时前
UI规范设计:从视觉到交互的全面指南
前端·ui·产品经理·设计规范·vibecoding