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。

相关推荐
我不是外星人1 小时前
有了 Harness Engineering ,真的还需要研发工程师吗?
前端·后端·ai编程
candyTong1 小时前
RTK 技术原理:一次典型会话里,80% 上下文是怎么省下来的
javascript·后端·架构
IT_陈寒4 小时前
JavaScript的闭包把我坑惨了,说好的内存会自动回收呢?
前端·人工智能·后端
Jackson__4 小时前
分享一个横向滚动案例,带悬停暂停,通用性很强
前端
MariaH5 小时前
git rebase的使用
前端
_柳青杨5 小时前
深入理解 JavaScript 事件循环
前端·javascript
阡陌Jony5 小时前
关于前端性能优化的一些问题:
前端
blanks20206 小时前
生成 公钥私钥 笔记
node.js
用户600071819106 小时前
【翻译】简化 TSRX
前端
IT乐手7 小时前
佛德角逼平西班牙,国足还有啥借口?
前端