惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

人人都是产品经理
人人都是产品经理
Stack Overflow Blog
Stack Overflow Blog
L
LINUX DO - 最新话题
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
Spread Privacy
Spread Privacy
www.infosecurity-magazine.com
www.infosecurity-magazine.com
雷峰网
雷峰网
Google DeepMind News
Google DeepMind News
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
S
Schneier on Security
B
Blog
The Register - Security
The Register - Security
SecWiki News
SecWiki News
Hacker News: Ask HN
Hacker News: Ask HN
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security Affairs
T
The Blog of Author Tim Ferriss
G
Google Developers Blog
T
Tenable Blog
P
Proofpoint News Feed
Apple Machine Learning Research
Apple Machine Learning Research
D
DataBreaches.Net
S
Secure Thoughts
Security Latest
Security Latest
H
Heimdal Security Blog
The Hacker News
The Hacker News
O
OpenAI News
AWS News Blog
AWS News Blog
量子位
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
腾讯CDC
U
Unit 42
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Hugging Face - Blog
Hugging Face - Blog
The Last Watchdog
The Last Watchdog
Recorded Future
Recorded Future
V2EX - 技术
V2EX - 技术
爱范儿
爱范儿
F
Full Disclosure

开飞机的老张

AGENTS.md Openspec 使用心得 从树莓派内网穿透到 Cloudflare Pages Openclaw和博客 郊眠寺 采石 译:我为什么使用Map(和WeakMap)处理DOM节点 介绍JavaScript中Symbol 原生JavaScript获取URL参数 字符串首字母大写 用forEach()遍历对象 JavaScript禁用按钮 JavaScript的FormData 用FileReader读取本地文件 JavaScript的Thenable JavaScript中Promise的reject JavaScript的立即调用函数表达式(IIFE) JavaScript的Promise链 用interact.js实现拖拽、缩放、吸附
JavaScript的Blob
kaifeiji.cc · 2023-07-25 · via 开飞机的老张

原文:Blobs in JavaScript

Blob(二进制大型对象)是JavaScript中一种类似于文件的数据结构,相当于客户端的Buffer。

Blob是一个包含任意字节的对象。Blob类是浏览器File API的一部分:JavaScript的File类是Blob的子类,所以当你从<input type="file">中获取一个文件实例时,它其实也是Blob的实例。

1
2
3
4
5
const input = document.querySelector('input[type="file"]');
const file = input.files[0];

file instanceof File;
file instanceof Blob;

FileReader一样,Blob类在各种浏览器中支持良好,但Node.js支持。Node.js的Buffer和Blob很相似,他们都存储任意字节,但它们时完全不同的API。

Blob有一个简洁的createObjectURL()函数,经常用在预览上传图片预览。给定一个Blob,URL.createObjectURL()创建一个指向该Blob的URL,可以直接用在srchref属性中。

例如,如果你点击以下文件input,选择一张图片,你会看到选择图片的预览效果。

静态博客无法显示,请查看原文

以下是这个input的JavaScript代码,使用URL.createObjectURL()来创建一个指向上传文件的本地URL,不需要将文件真正上传到服务器。

1
2
3
4
5
6
7
8
9
10
const input = document.querySelector('#data-url-example');

input.addEventListener('change', () => {
const file = input.files[0];
const url = URL.createObjectURL(file);

document.querySelector('#data-url-container').innerHTML = `
<img src="${url}" />
`;
});

本教程对您有帮助吗?来GitHub仓库点个星支持我们吧!