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。

相关推荐
小王码农记几秒前
vue2中table插槽新语法 v-slot
前端·vue.js
前端婴幼儿5 分钟前
前端直接下载到本地(实时显示下载进度)
前端
三小河5 分钟前
前端 Class 语法从 0 开始学起
前端
hjt_未来可期9 分钟前
js实现复制、粘贴文字
前端·javascript·html
米诺zuo11 分钟前
Next.js 路由与中间件
前端
小明记账簿_微信小程序11 分钟前
webpack实用配置dev--react(一)
前端
videring15 分钟前
打字机效果-支持ckeditor5、框架无关
前端·javascript
tianxia15 分钟前
主项目通过iframe嵌套子项目,子项目弹框无法全屏
前端·javascript
米诺zuo15 分钟前
node版本管理nvs
前端
ohyeah16 分钟前
AI First 时代:用大模型构建轻量级后台管理系统
前端·llm