当前位置:网站首页>Blob 对象介绍
Blob 对象介绍
2022-07-07 04:58:00 【丢丢的大神】
Blob 对象表示一个不可变、原始数据的类文件对象。它的数据可以按文本或二进制的格式进行读取,也可以转换成 ReadableStream 来用于数据操作。
看下面代码,可以使用文本构造 Blob 对象
var debug = {
hello: "world"};
var blob = new Blob([JSON.stringify(debug, null, 2)], {
type : 'application/json'});
示例:使用 Blob 创建一个指向类型化数组的 URL
var typedArray = GetTheTypedArraySomehow();
var blob = new Blob([typedArray.buffer], {
type: 'application/octet-stream'}); // 传入一个合适的 MIME 类型
var url = URL.createObjectURL(blob);
// 会产生一个类似 blob:d3958f5c-0777-0845-9dcf-2cb28783acaf 这样的 URL 字符串
// 你可以像使用普通 URL 那样使用它,比如用在 img.src 上。
另一种读取 Blob 中内容的方式是使用 Response 对象。
var text = await (new Response(blob)).text();
某些时候后台下载文件返回到前端被转换为 Blob 对象,正常下载时,返回正确的文件流,异常时,后台返回一段包含错误信息的 json ,这时候需要提示用户下载失败,看下面代码
/** * 格式化 blob 返回值 * @param {*} res */
const formatBlobResponse = async (res) => {
const text = await new Response(res).text();
let textObj = {
};
try {
textObj = JSON.parse(text);
} catch (e) {
}
if (text === '' || (textObj.code && Number(textObj.code) !== 200)) {
return {
success: false,
msg: textObj.msg || '下载文件失败,请联系管理员',
};
} else {
return {
success: true,
data: res,
};
}
};
边栏推荐
- 2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions
- Installing postgresql11 database under centos7
- 青龙面板--整理能用脚本
- 有 Docker 谁还在自己本地安装 Mysql ?
- [VHDL parallel statement execution]
- 【踩坑系列】uniapp之h5 跨域的问题
- Network learning (III) -- highly concurrent socket programming (epoll)
- Recursive construction of maximum binary tree
- QT learning 28 toolbar in the main window
- Qt学习28 主窗口中的工具栏
猜你喜欢

【数字IC验证快速入门】17、SystemVerilog学习之基本语法4(随机化Randomization)

LeetCode中等题之我的日程安排表 I

2022 Inner Mongolia latest advanced fire facility operator simulation examination question bank and answers

Li Kou interview question 04.01 Path between nodes

Empire CMS collection Empire template program general

Implementation of replacement function of shell script

Explore dry goods! Apifox construction ideas

Problem solving: unable to connect to redis

3D reconstruction - stereo correction

You Li takes you to talk about C language 6 (common keywords)
随机推荐
Chip information website Yite Chuangxin
互动送书-《Oracle DBA工作笔记》签名版
Rust versus go (which is my preferred language?)
Thinkcmf6.0安装教程
Empire CMS collection Empire template program general
Dedecms collects content without writing rules
Lattice coloring - matrix fast power optimized shape pressure DP
Recursive method to construct binary tree from preorder and inorder traversal sequence
[quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)
Linux server development, MySQL stored procedures, functions and triggers
【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)
[CV] Wu Enda machine learning course notes | Chapter 8
C语言队列
Open source ecosystem | create a vibrant open source community and jointly build a new open source ecosystem!
Roulette chart 2 - writing of roulette chart code
buureservewp(2)
Es FAQ summary
Quickly use Jacobo code coverage statistics
C language queue
Padavan manually installs PHP