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。

相关推荐
Vic Rattlehead15 分钟前
HTML5+CSS3-HTML5入门
前端·css3·html5
Xz _Yang1 小时前
React中常见Hooks总结
前端·javascript·react.js
shiming88791 小时前
React Navigation 优质的开源项目
前端·react.js·开源
努力的派大星星1 小时前
【Material-UI】Button 组件自定义详解
开发语言·javascript·ui·material-ui
全宇宙最最帅气的哆啦A梦小怪兽1 小时前
vite.config.js文件代码解释
开发语言·javascript·ecmascript
代码对我眨眼睛1 小时前
牛客JS题(二十三)判断质数
前端·javascript·刷题·js进阶
败者_食尘1 小时前
前端(HTML + CSS)学成在线项目(仿)
前端·css·html
米柆1 小时前
css水波浪动画效果
前端·css
vx_bscxy3221 小时前
node.js蒲公英旅游系统—计算机毕业设计源码15565
java·spring boot·后端·node.js·php·课程设计·旅游
xingba2 小时前
IntersectionObserver + scrollIntoView 实现电梯导航
前端·javascript·css