当前位置:网站首页>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年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案
- Problem solving: unable to connect to redis
- Network learning (I) -- basic model learning
- [matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead
- Linux server development, redis source code storage principle and data model
- LeetCode 40:组合总和 II
- 快速使用 Jacoco 代码覆盖率统计
- padavan手动安装php
- 【VHDL 并行语句执行】
- 2022 welder (elementary) judgment questions and online simulation examination
猜你喜欢
【數字IC驗證快速入門】15、SystemVerilog學習之基本語法2(操作符、類型轉換、循環、Task/Function...內含實踐練習)
These five fishing artifacts are too hot! Programmer: I know, delete it quickly!
Content of string
Network learning (I) -- basic model learning
Figure out the working principle of gpt3
Leetcode 90: subset II
Custom class loader loads network class
Record a stroke skin bone error of the skirt
探索干货篇!Apifox 建设思路
LeetCode 40:组合总和 II
随机推荐
Li Kou interview question 04.01 Path between nodes
[quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)
[UVM basics] summary of important knowledge points of "UVM practice" (continuous update...)
Rust versus go (which is my preferred language?)
2022 National latest fire-fighting facility operator (primary fire-fighting facility operator) simulation questions and answers
这5个摸鱼神器太火了!程序员:知道了快删!
Recursive method to verify whether a tree is a binary search tree (BST)
Who has docker to install MySQL locally?
芯片 设计资料下载
Cnopendata American Golden Globe Award winning data
Chip design data download
[Matlab] Simulink 自定义函数中的矩阵乘法工作不正常时可以使用模块库中的矩阵乘法模块代替
Dedecms collects content without writing rules
Problem solving: unable to connect to redis
[VHDL parallel statement execution]
【无标题】
Numbers that appear only once
Recursive method constructs binary tree from middle order and post order traversal sequence
Content of string
Quickly use Jacobo code coverage statistics