当前位置: 首页 > news >正文

原生js上传图片

无样式上传图片

创建一个 FormData 对象:这个对象可以用于存储数据。

将文件添加到 FormData 对象:通过 append() 方法,将用户选择的文件添加到 formData 对象中。

使用 fetch 发送请求:使用 fetch API 或者其他方法将 FormData 对象发送到服务器。

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>图片上传</title></head><body><!-- 文件上传 --><input type="file" id="fileInput" onchange="uploadImage()" /><script>// 上传图片的函数function uploadImage() {// 获取文件输入框元素const fileInput = document.getElementById("fileInput");// 创建formData对象,用于存储要上传的文件数据const formData = new FormData();formData.append("file", fileInput.files[0]); // 将选中的文件添加到formData对象中// 请求接口fetch("http://yikatong.kuxia.top/pc/common/upload", {method: "POST",body: formData, // 请求体是formData对象,其中包含文件数据}).then((response) => response.json()) // 服务器响应成功后,解析JSON格式的响应数据.then((data) => {console.log("上传成功:", data);}).catch((error) => {console.error("上传失败:", error);});}</script></body>
</html>

有样式的上传图片

 就是把图片回显的地方盖着输入框

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>图片上传</title></head><style>* {padding: 0;margin: 0;}.box {display: flex;}/* 添加虚线边框 */#image {width: 100px;height: 100px;border-radius: 5px;margin-left: -100px;}</style><body><div class="box"><!-- 文件上传 --><inputtype="file"id="fileInput"onchange="uploadImage()"style="height: 100px; width: 100px; opacity: 0"/><!-- 用于回显图片 --><img id="image" src="/img/add.svg" /></div><script>const fileInput = document.getElementById("fileInput"); // 获取上传文件框const image = document.getElementById("image"); // 获取回显图片框function uploadImage() {const formData = new FormData(); // 创建formData对象,用于存储要上传的文件数据formData.append("file", fileInput.files[0]); // 将选中的文件添加到formData对象中// 请求接口fetch("http://yikatong.kuxia.top/pc/common/upload", {method: "POST",body: formData, // 请求体是formData对象,其中包含文件数据}).then((response) => response.json()) // 服务器响应成功后,解析JSON格式的响应数据.then((data) => {console.log("上传成功:", data.data.url);// 回显图片image.src = "http://yikatong.kuxia.top" + data.data.url;}).catch((error) => {console.error("上传失败:", error);});}</script></body>
</html>

http://www.lryc.cn/news/495103.html

相关文章:

  • 使用torch==2.5.1版本用的清华源遇到的坑
  • 泷羽Sec-星河飞雪-BurpSuite之解码、日志、对比模块基础使用
  • 对拍详细使用方法
  • Python面向对象编程与模块化设计练习
  • Linux系统硬件老化测试脚本:自动化负载与监控
  • 搭建一个基于Web的文档管理系统,用于存储、共享和协作编辑文档
  • 排序学习整理(1)
  • 《深入探究 Java 中的 boolean 类型》
  • 智享 AI 自动无人直播系统:打破地域与时间枷锁中小微企业的营销破局利器
  • 接口测试工具:reqable
  • 同时多平台git配置:GitHub和Gitee生成不同的SSH Key
  • 刷题计划day24 回溯(三)【复原 IP 地址】【子集】【子集 II】
  • 从“找三角形”讲“等腰三角形”
  • Java中的泛型方法和泛型类
  • springboot学习-spring-boot-data-jdbc分页/排序/多表查询的例子
  • 通信与网络基础
  • 【3.存储系统】综合大题
  • 【Linux】【字符设备驱动】深入解析
  • 【JavaEE】多线程(2)
  • mac下Gpt Chrome升级成GptBrowser书签和保存的密码恢复
  • 使用Grafana K6来测测你的系统负载能力
  • 【论文复现】基于BERT的语义分析实现
  • CTF-RE: STL逆向 [NewStarCTF 2023 公开赛道 STL] WP
  • 实习冲刺第三十六天
  • 【Zemax光学设计实训三】---激光缩束镜的设计优化
  • TCP/IP协议簇自学笔记
  • Spring Boot教程之十一:获取Request 请求 和 Put请求
  • 计算机网络(二)
  • 如何在Python中进行数学建模?
  • JavaSE——类与对象(5)