Chromium 中JavaScript FileReader API接口c++代码实现

FileReader

备注: 此特性在 Web Worker 中可用。

FileReader 接口允许 Web 应用程序异步读取存储在用户计算机上的文件(或原始数据缓冲区)的内容,使用 File 或 Blob 对象指定要读取的文件或数据。

文件对象可以从用户使用 <input> 元素选择文件而返回的 FileList 对象中获取,或者从拖放操作的 DataTransfer 对象中获取。

FileReader 只能访问用户明确选择的文件内容,无论是使用 HTML <input type="file"> 元素还是通过拖放。它不能用于从用户的文件系统中按路径名读取文件。要按路径名读取客户端文件系统上的文件,请使用文件系统访问 API。要读取服务器端文件,请使用 fetch(),如果跨源读取,则需要 CORS 权限。

EventTargetFileReader

构造函数

FileReader()

返回一个新的 FileReader 对象。

有关详细信息和示例,请参阅在 Web 应用程序中使用文件。

实例属性

FileReader.error 只读

一个表示在读取文件时发生的错误的 DOMException。

FileReader.readyState 只读

表示FileReader状态的数字。取值如下:

常量名描述
EMPTY0还没有加载任何数据。
LOADING1数据正在被加载。
DONE2已完成全部的读取请求。

FileReader.result 只读

文件的内容。该属性仅在读取操作完成后才有效,数据的格式取决于使用哪个方法来启动读取操作。

实例方法

FileReader.abort()

中止读取操作。在返回时,readyState 属性为 DONE

FileReader.readAsArrayBuffer()

开始读取指定的 Blob 中的内容,一旦完成,result 属性中将包含一个表示文件数据的 ArrayBuffer 对象。

FileReader.readAsBinaryString() 已弃用

开始读取指定的 Blob 中的内容。一旦完成,result 属性中将包含一个表示文件中的原始二进制数据的字符串。

FileReader.readAsDataURL()

开始读取指定的 Blob 中的内容。一旦完成,result 属性中将包含一个表示文件数据的 data: URL。

FileReader.readAsText()

开始读取指定的 Blob 中的内容。一旦完成,result 属性中将包含一个表示所读取的文件内容的字符串。可以指定可选的编码名称。

事件

使用 addEventListener() 方法或通过将事件侦听器分配给此接口的 oneventname 属性来侦听这些事件。一旦不再使用 FileReader,请使用 removeEventListener() 删除事件侦听器,以避免内存泄漏。

abort

当读取被中止时触发,例如因为程序调用了 FileReader.abort() 方法。

error

当读取由于错误而失败时触发。

load

读取成功完成时触发。

loadend

读取完成时触发,无论成功与否。

loadstart

读取开始时触发。

progress

读取数据时定期触发。

前端中这些接口在c++中代码实现如下:

1、FileReader接口定义 third_party\blink\renderer\core\fileapi\file_reader.idl

/** Copyright (C) 2010 Google Inc.  All rights reserved.* Copyright (C) 2011 Apple Inc. All rights reserved.** Redistribution and use in source and binary forms, with or without* modification, are permitted provided that the following conditions are* met:**     * Redistributions of source code must retain the above copyright* notice, this list of conditions and the following disclaimer.*     * Redistributions in binary form must reproduce the above* copyright notice, this list of conditions and the following disclaimer* in the documentation and/or other materials provided with the* distribution.*     * Neither the name of Google Inc. nor the names of its* contributors may be used to endorse or promote products derived from* this software without specific prior written permission.** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/// https://w3c.github.io/FileAPI/#APIASynch[ActiveScriptWrappable,Exposed=(Window,Worker)
] interface FileReader : EventTarget {[CallWith=ExecutionContext] constructor();// async read methods[RaisesException] void readAsArrayBuffer(Blob blob);[RaisesException] void readAsBinaryString(Blob blob);[RaisesException] void readAsText(Blob blob, optional DOMString label);[RaisesException] void readAsDataURL(Blob blob);void abort();// statesconst unsigned short EMPTY = 0;const unsigned short LOADING = 1;const unsigned short DONE = 2;[ImplementedAs=getReadyState] readonly attribute unsigned short readyState;// File or Blob datareadonly attribute (DOMString or ArrayBuffer)? result;readonly attribute DOMException? error;// event handler attributesattribute EventHandler onloadstart;attribute EventHandler onprogress;attribute EventHandler onload;attribute EventHandler onabort;attribute EventHandler onerror;attribute EventHandler onloadend;
};

2、FileReader接口实现

third_party\blink\renderer\core\fileapi\file_reader.h

third_party\blink\renderer\core\fileapi\file_reader.cc

/** Copyright (C) 2010 Google Inc.  All rights reserved.** Redistribution and use in source and binary forms, with or without* modification, are permitted provided that the following conditions are* met:**     * Redistributions of source code must retain the above copyright* notice, this list of conditions and the following disclaimer.*     * Redistributions in binary form must reproduce the above* copyright notice, this list of conditions and the following disclaimer* in the documentation and/or other materials provided with the* distribution.*     * Neither the name of Google Inc. nor the names of its* contributors may be used to endorse or promote products derived from* this software without specific prior written permission.** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_FILEAPI_FILE_READER_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_FILEAPI_FILE_READER_H_#include "base/timer/elapsed_timer.h"
#include "third_party/blink/renderer/bindings/core/v8/active_script_wrappable.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/dom/events/event_target.h"
#include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
#include "third_party/blink/renderer/core/fileapi/file_error.h"
#include "third_party/blink/renderer/core/fileapi/file_read_type.h"
#include "third_party/blink/renderer/core/fileapi/file_reader_client.h"
#include "third_party/blink/renderer/core/fileapi/file_reader_loader.h"
#include "third_party/blink/renderer/core/probe/async_task_context.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/wtf/forward.h"namespace blink {class Blob;
class ExceptionState;
class ExecutionContext;
class V8UnionArrayBufferOrString;
enum class FileErrorCode;class CORE_EXPORT FileReader final : public EventTarget,public ActiveScriptWrappable<FileReader>,public ExecutionContextLifecycleObserver,public FileReaderAccumulator {DEFINE_WRAPPERTYPEINFO();public:static FileReader* Create(ExecutionContext*);explicit FileReader(ExecutionContext*);~FileReader() override;enum ReadyState { kEmpty = 0, kLoading = 1, kDone = 2 };void readAsArrayBuffer(Blob*, ExceptionState&);void readAsBinaryString(Blob*, ExceptionState&);void readAsText(Blob*, const String& encoding, ExceptionState&);void readAsText(Blob*, ExceptionState&);void readAsDataURL(Blob*, ExceptionState&);void abort();ReadyState getReadyState() const { return state_; }DOMException* error() { return error_.Get(); }V8UnionArrayBufferOrString* result() const;probe::AsyncTaskContext* async_task_context() { return &async_task_context_; }// ExecutionContextLifecycleObservervoid ContextDestroyed() override;// ScriptWrappablebool HasPendingActivity() const final;// EventTargetconst AtomicString& InterfaceName() const override;ExecutionContext* GetExecutionContext() const override {return ExecutionContextLifecycleObserver::GetExecutionContext();}// FileReaderClientFileErrorCode DidStartLoading() override;FileErrorCode DidReceiveData() override;void DidFinishLoading(FileReaderData contents) override;void DidFail(FileErrorCode) override;DEFINE_ATTRIBUTE_EVENT_LISTENER(loadstart, kLoadstart)DEFINE_ATTRIBUTE_EVENT_LISTENER(progress, kProgress)DEFINE_ATTRIBUTE_EVENT_LISTENER(load, kLoad)DEFINE_ATTRIBUTE_EVENT_LISTENER(abort, kAbort)DEFINE_ATTRIBUTE_EVENT_LISTENER(error, kError)DEFINE_ATTRIBUTE_EVENT_LISTENER(loadend, kLoadend)void Trace(Visitor*) const override;private:class ThrottlingController;void Terminate();void ReadInternal(Blob*, FileReadType, ExceptionState&);void FireEvent(const AtomicString& type);void ExecutePendingRead();ReadyState state_;// Internal loading state, which could differ from ReadyState as it's// for script-visible state while this one's for internal state.enum LoadingState {kLoadingStateNone,kLoadingStatePending,kLoadingStateLoading,kLoadingStateAborted};LoadingState loading_state_;bool still_firing_events_;String blob_type_;scoped_refptr<BlobDataHandle> blob_data_handle_;FileReadType read_type_;String encoding_;probe::AsyncTaskContext async_task_context_;Member<FileReaderLoader> loader_;Member<V8UnionArrayBufferOrString> result_ = nullptr;Member<DOMException> error_;absl::optional<base::ElapsedTimer> last_progress_notification_time_;
};}  // namespace blink#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_FILEAPI_FILE_READER_H_

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1556085.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

【微服务】服务注册与发现 - Eureka(day3)

CAP理论 P是分区容错性。简单来说&#xff0c;分区容错性表示分布式服务中一个节点挂掉了&#xff0c;并不影响其他节点对外提供服务。也就是一台服务器出错了&#xff0c;仍然可以对外进行响应&#xff0c;不会因为某一台服务器出错而导致所有的请求都无法响应。综上所述&…

实验4 循环结构

1、判断素数 【问题描述】从键盘输入一个大于1的正整数&#xff0c;判断是否为素数 【输入形式】输入一个正整数 【输出形式】输出该数是否为素数 【样例输入】10 【样例输出】10 is not a prime number 【样例说明】样例2 输入&#xff1a;-10 输出&#xff1a;error! #de…

jmeter学习(7)beanshell

beanshell preprocessor 发送请求前执行 beanshell postprocessor 发送请求前执行 获取请求相关信息 String body sampler.getArguments().getArgument(0).getValue(); String url sampler.getPath(); 获取响应报文 String responseprev.getResponseDataAsString(); 获…

北京自闭症寄宿学校大盘点:优质教育资源汇总

北京自闭症寄宿学校大盘点&#xff1a;优质教育资源中的璀璨明珠——兼谈广州星贝育园 在北京&#xff0c;随着社会对自闭症儿童教育的日益重视&#xff0c;越来越多的优质寄宿学校应运而生&#xff0c;为这些特殊的孩子提供了专业的康复与教育环境。然而&#xff0c;当我们把…

【数据结构】【链表代码】随机链表的复制

/*** Definition for a Node.* struct Node {* int val;* struct Node *next;* struct Node *random;* };*/typedef struct Node Node; struct Node* copyRandomList(struct Node* head) {if(headNULL)return NULL;//1.拷贝结点&#xff0c;连接到原结点的后面Node…

猫头虎深度解读:过去2周,AI领域的十大突破事件与未来展望

猫头虎深度解读&#xff1a;过去2周&#xff0c;AI领域的十大突破事件与未来展望 &#x1f680;&#x1f916; 大家好&#xff0c;我是猫头虎技术团队的代表&#xff01;这两周&#xff0c;人工智能领域再次掀起了技术与应用的新浪潮。从立法到技术进展&#xff0c;再到前沿应…

初始爬虫12(反爬与反反爬)

学到这里&#xff0c;已经可以开始实战项目了&#xff0c;多去爬虫&#xff0c;了解熟悉反爬&#xff0c;然后自己总结出一套方法怎么做。 1.服务器反爬的原因 服务器反爬的原因 总结&#xff1a; 1.爬虫占总PV较高&#xff0c;浪费资源 2.资源被批量抓走&#xff0c;丧失竞争力…

交叉熵的数学推导和手撕代码

交叉熵的数学推导和手撕代码 数学推导手撕代码 数学推导 手撕代码 import torch import torch.nn.functional as F# 二元交叉熵损失函数 def binary_cross_entropy(predictions, targets):# predictions应为sigmoid函数的输出&#xff0c;即概率值# targets应为0或1的二进制标…

MathType软件7.7最新版本下载安装教程+使用深度评测

嘿&#xff0c;各位亲爱的朋友们&#xff01;&#x1f44b; 今天我要来给大家安利一个神器——MathType软件&#xff01;&#x1f389; 如果你是一位学生、老师或者需要经常和数学公式打交道的科研工作者&#xff0c;那这个软件绝对是你的不二选择&#xff01;&#x1f60e; M…

ctf.bugku-备份是个好习惯

访问页面得到字符串 这串字符串是重复的&#xff1b; d41d8cd98f00b204e9800998ecf8427e 从前端、源码上看&#xff0c;除了这段字符串&#xff0c;没有其他信息&#xff1b;尝试解密&#xff0c;长度32位&#xff1b;各种解密方式试试&#xff1b; MD5免费在线解密破解_MD5在…

市面上8款AI论文大纲一键生成文献的软件推荐

在当前的学术研究和写作领域&#xff0c;AI论文大纲自动生成软件已经成为提高写作效率和质量的重要工具。这些工具不仅能够帮助研究人员快速生成论文草稿&#xff0c;还能进行内容优化、查重和排版等操作。本文将分享市面上8款AI论文大纲一键生成文献的软件&#xff0c;并特别推…

[git] github管理项目之环境依赖管理

导出依赖到 requirements.txt pip install pipreqs pipreqs . --encodingutf8 --force但是直接使用pip安装不了torch&#xff0c;需要添加源&#xff01;&#xff01; pip install -r requirements.txt -f https://download.pytorch.org/whl/torch_stable.htmlpython 项目中 …

Stable Diffusion绘画 | AI 图片智能扩充,超越PS扩图的AI扩图功能(附安装包)

来到「文生图」页面&#xff0c;输入固定的起手式提示词。 第1步&#xff0c;开启 ControlNet&#xff0c;将需要扩充的图片加载进来&#xff1a; 控制类型选择「Inpaint」&#xff0c;预处理器选择「inpaint_onlylama」&#xff0c;缩放模式选择「缩放后填充空白」&#xff1…

【数据结构】【链表代码】移除链表元素

移除链表元素 /*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/struct ListNode* removeElements(struct ListNode* head, int val) { // 创建一个虚拟头节点&#xff0c;以处理头节点可能被删除的情况 struct…

代码随想录Day54

今天是国庆假期后的恢复做题的第一天&#xff0c;摆了那么久感觉还是有点没摆够哈哈哈哈&#xff01;今天两道题都是困难题&#xff0c;两道题都去看讲解了&#xff0c;感觉这两道题是高度相似的&#xff0c;接雨水用单调递增栈来做&#xff0c;柱状图中最大的矩形用单调递减栈…

tcp/ip、以太网、mqtt、modbus/tcp复习

1.osi参考模型 2. modbus是应用层报文传输协议&#xff0c;没有规定物理层&#xff0c;只规定了协议帧&#xff0c;但是定义了控制器能够认识和使用的消息结构&#xff0c;不管它们是经过何种网络进行通信的&#xff0c;具有很强的适应性。 一主多从&#xff0c;同一时间主机…

【动态规划-最长公共子序列(LCS)】力扣1035. 不相交的线

在两条独立的水平线上按给定的顺序写下 nums1 和 nums2 中的整数。 现在&#xff0c;可以绘制一些连接两个数字 nums1[i] 和 nums2[j] 的直线&#xff0c;这些直线需要同时满足&#xff1a; nums1[i] nums2[j] 且绘制的直线不与任何其他连线&#xff08;非水平线&#xff09…

Rethinking Graph Neural Networksfor Anomaly Detection

AAAI24 推荐指数 #paper/⭐⭐ (由于这个领域初读&#xff0c;因此给的推荐分可能不好) 个人总结&#xff1a; 其在半监督&#xff08;1%&#xff0c;40%&#xff09;的情况下&#xff0c;使用多通滤波器&#xff0c;将不同滤波器得到的特征拼接起来&#xff0c;来做分类&…

【Blender Python】2.结合Kimi生成

概述 结合Kimi这样的AI工具可以生成Blender Python代码&#xff0c;用来辅助生成一些或简单或复杂的图形。当然&#xff0c;出不出错这就不一定了。因为AI所训练的版本可能并不是Blender的最新版本&#xff0c;类似的问题也出现在Godot上。 测试 在kimi中提问&#xff0c;获…

2024/10/6周报

文章目录 摘要Abstract广西的一些污水处理厂工艺解析1. A/O工艺&#xff08;厌氧-缺氧-好氧工艺&#xff09;2. 氧化沟工艺3. MBR工艺&#xff08;膜生物反应器&#xff09;4. SBR工艺&#xff08;序批式活性污泥法&#xff09;5. 生物接触氧化法 其它补充一体化改良氧化沟工艺…