This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

Expo Blob

一个符合 Web 标准的 React Native Blob 实现。

Android
iOS
Web
Included in Expo Go

expo-blob 为 React Native 提供了一个符合网页标准的 Blob 实现,具有更高的性能,并且在所有平台上都能稳定运行。与从 react-native 导出的实现相比,它更可靠,而 react-native 的 Blob 存在 slice() 方法及其他 Web API 功能上的限制。

安装

🌐 Installation

Terminal
- npx expo install expo-blob
- yarn expo install expo-blob
- pnpm expo install expo-blob
- bun expo install expo-blob

If you are installing this in an existing React Native app, make sure to install expo in your project.

用法

🌐 Usage

基本 Blob 创建

🌐 Basic blob creation

import { Blob } from 'expo-blob'; // Create an empty blob const emptyBlob = new Blob(); // Create a blob from text const textBlob = new Blob(['Hello, World!'], { type: 'text/plain' }); // Create a blob from binary data const binaryBlob = new Blob([new Uint8Array([1, 2, 3, 4])], { type: 'application/octet-stream', }); // Create a blob from mixed content const mixedBlob = new Blob( [ 'Text content', new Uint8Array([65, 66, 67]), // ABC in ASCII 'More text', ], { type: 'text/plain' } );

Blob 属性

🌐 Blob properties

const blob = new Blob(['Hello, World!'], { type: 'text/plain' }); console.log(blob.size); // 13 (bytes) console.log(blob.type); // "text/plain"

读取 Blob 内容

🌐 Reading blob content

const blob = new Blob(['Hello, World!'], { type: 'text/plain' }); // Read as text const text = await blob.text(); console.log(text); // "Hello, World!" // Read as bytes const bytes = await blob.bytes(); console.log(bytes); // Uint8Array(13) [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33] // Read as ArrayBuffer const arrayBuffer = await blob.arrayBuffer(); console.log(arrayBuffer); // ArrayBuffer(13)

切片 Blob

🌐 Slicing blobs

const blob = new Blob(['Hello, World!'], { type: 'text/plain' }); // Slice from position 0 to 5 const slice1 = blob.slice(0, 5); console.log(await slice1.text()); // "Hello" // Slice from position 7 to end const slice2 = blob.slice(7); console.log(await slice2.text()); // "World!" // Slice with custom type const slice3 = blob.slice(0, 5, 'text/html'); console.log(slice3.type); // "text/html"

流式

🌐 Streaming

const blob = new Blob(['Large content...'], { type: 'text/plain' }); // Create a readable stream const stream = blob.stream(); const reader = stream.getReader(); // Read chunks while (true) { const { done, value } = await reader.read(); if (done) break; console.log('Chunk:', value); }

应用接口

🌐 API

No API data file found, sorry!