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

JavaScript将url转为blob和file,三种方法

预览地址

https://code.juejin.cn/pen/7561304070028279818

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Document</title></head><body><div style="margin: 20px 0"><input type="text" placeholder="请输入URL" style="width: 600px" /></div><button class="button1">xhr请求</button><button class="button2">fetch请求</button><button class="button3">img请求</button></body><script>let btn1 = document.querySelector(".button1");let btn2 = document.querySelector(".button2");let btn3 = document.querySelector(".button3");btn1.onclick = function () {let url = document.querySelector("input").value;url += "?" + new Date().getTime();urlToFile(url);};btn2.onclick = function () {let url = document.querySelector("input").value;url += "?" + new Date().getTime();fetchUrlToFile(url);};// 添加btn3的点击事件处理btn3.onclick = function () {let url = document.querySelector("input").value;url += "?" + new Date().getTime();imgUrlToFile(url);};// 通过xhrfunction urlToFile(url) {// 创建XMLHttpRequest对象const xhr = new XMLHttpRequest();// 配置请求xhr.open("GET", url, true);xhr.responseType = "blob"; // 指定响应类型为blob// 处理请求完成xhr.onload = function () {if (xhr.status === 200 && xhr.readyState === 4) {const blob = xhr.response;// 从URL中提取文件名const fileName = url.split("/").pop() || "image.jpg";// 创建文件对象const file = new File([blob], fileName, {type: "image/" + url.split(".").pop(),});console.log(file);}};// 发送请求xhr.send();}// 通过fetchfunction fetchUrlToFile(url) {fetch(url).then((response) => response.blob()).then((blob) => {// 从URL中提取文件名const fileName = url.split("/").pop() || "image.jpg";// 创建文件对象const file = new File([blob], fileName, {type: "image/" + url.split(".").pop(),});console.log(file);});}// 通过img标签function imgUrlToFile(url) {const img = new Image();img.crossOrigin = "Anonymous"; // 处理跨域问题// 开始加载图片img.src = url;img.onload = function () {// 创建canvas元素const canvas = document.createElement("canvas");canvas.width = img.width;canvas.height = img.height;// 获取2D上下文并绘制图片const ctx = canvas.getContext("2d");ctx.drawImage(img, 0, 0);// 将canvas转换为blobcanvas.toBlob(function (blob) {// 从URL中提取文件名const fileName = url.split("/").pop() || "image.jpg";// 创建文件对象const file = new File([blob], fileName, {type: "image/" + (url.split(".").pop() || "jpg"),});console.log(file);}, "image/" + (url.split(".").pop() || "jpg"));};}</script>
</html>
http://www.dtcms.com/a/486929.html

相关文章:

  • 电商营销型网站建设中国菲律宾关系现状
  • 英文网站建设 飞沐wordpress公众号文章分类
  • 怎么做qq靓号网站岳阳网站设计公司
  • Unity 通过Texture生成的Sprite存在边缘黑线问题 Image黑边问题
  • 计算机方向如何才能更好的找到工作?(成长心得)
  • 大连市城市建设投资集团网站网站怎么做文件上传
  • 织梦网站转跳手机站盐城网盐城网站建设站建设
  • Spring AOP 中@annotation的两种写法详解
  • Linux设备模型
  • Linux fg命令使用教程
  • 微博爬虫流程解析——session的使用
  • 企业网站建设的核心是专业手机建站公司
  • Vllm Semantic-router MoM 架构
  • LLMs-from-scratch :KV 缓存
  • dshot协议数据帧格式
  • 华清远见25072班QT学习day1
  • 一个网站多个子域名优化网站系统建设与管理
  • SpringBoot使用LocalDate接收参数解析问题
  • 推荐系统学习笔记(十八)SECR-快手-用户留存建模
  • 自助网站建设技术支持建商城网站需要什么
  • 物联网设备安全接入管理系统的设计与核心功能
  • 在React中使用函数式编程的优点和缺点是什么?
  • UE5 Windows平台下打包Linux
  • UE5 建模专题 - 2:地形模式里的工具栏里的按钮的使用,以及样条点制作道路
  • 基于PHP+Mysql的旅游介绍网
  • HOW - prefetch 二级页面实践
  • OCR:开启档案管理的智能新时代
  • 设计网站官网入口广州做一个网站多少钱
  • 比较好的平面设计网站群晖nas安装wordpress安装
  • 重生之我在大学自学鸿蒙开发第六天-《组件导航》