input上传--upload

1.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

*{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

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. vue2 upload多图片

    2024-07-13 08:32:05       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 08:32:05       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 08:32:05       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 08:32:05       57 阅读
  4. Python语言-面向对象

    2024-07-13 08:32:05       68 阅读

热门阅读

  1. 网络设备安全

    2024-07-13 08:32:05       23 阅读
  2. sqlalchemy.orm中validates对两个字段进行联合校验

    2024-07-13 08:32:05       27 阅读
  3. Grafana

    Grafana

    2024-07-13 08:32:05      23 阅读
  4. VB 实例:掌握 Visual Basic 编程的精髓

    2024-07-13 08:32:05       21 阅读
  5. Spuer().__init__的意义

    2024-07-13 08:32:05       29 阅读
  6. 匿名函数与函数

    2024-07-13 08:32:05       28 阅读
  7. ios CCRuntime.m

    2024-07-13 08:32:05       23 阅读
  8. js项目生产环境中移除 console

    2024-07-13 08:32:05       24 阅读
  9. uniapp微信小程序授权登录实现

    2024-07-13 08:32:05       24 阅读
  10. 版本发布 | IvorySQL 3.3 发版

    2024-07-13 08:32:05       26 阅读
  11. 【分布式系统】Ceph对象存储系统之RGW接口

    2024-07-13 08:32:05       27 阅读
  12. 浅谈PostCSS

    2024-07-13 08:32:05       26 阅读
  13. AI学习指南机器学习篇-层次聚类的优缺点

    2024-07-13 08:32:05       23 阅读