FileReaderSync
备注: 此特性仅在 Web Worker(不包括 Service Worker)中可用。
FileReaderSync
接口允许同步读取 File 或 Blob 对象。此接口仅在 worker 中可用,因为它支持可能导致潜在的阻塞的同步 I/O。
构造函数
返回一个新的 FileReaderSync
对象。
实例属性
该接口没有任何属性。
实例方法
FileReaderSync.readAsArrayBuffer()
此方法将指定的 Blob 或 File 转换为将输入数据表示为二进制字符串的 ArrayBuffer。
FileReaderSync.readAsBinaryString() 已弃用
此方法将指定的 Blob 或 File 转换为表示输入数据的二进制字符串。此方法已弃用,请考虑使用 readAsArrayBuffer()
代替。
此方法将指定的 Blob 或 File 转换为将输入数据表示为文本字符串的字符串。可选的 encoding
参数指示要使用的编码(例如,iso-8859-1 或 UTF-8)。如果不存在,该方法将对其应用检测算法以确定其编码。
FileReaderSync.readAsDataURL()
此方法将指定的 Blob 或 File 转换为将输入数据表示为 data URL 的字符串。
FileReaderSync在c++代码中实现:
1、FileReaderSync接口定义 third_party\blink\renderer\core\fileapi\file_reader_sync.idl
/*
* 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.
*/
// https://w3c.github.io/FileAPI/#FileReaderSync
[
Exposed=(DedicatedWorker,SharedWorker)
] interface FileReaderSync {
[CallWith=ExecutionContext, Measure] constructor();
[RaisesException] ArrayBuffer readAsArrayBuffer(Blob blob);
[RaisesException] DOMString readAsBinaryString(Blob blob);
[RaisesException] DOMString readAsText(Blob blob, optional DOMString label);
[RaisesException] DOMString readAsDataURL(Blob blob);
};
2、FileReaderSync接口接口实现
third_party\blink\renderer\core\fileapi\file_reader_sync.h
third_party\blink\renderer\core\fileapi\file_reader_sync.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_SYNC_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_FILEAPI_FILE_READER_SYNC_H_
#include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
#include "third_party/blink/renderer/core/fileapi/file_reader_client.h"
#include "third_party/blink/renderer/core/fileapi/file_reader_data.h"
#include "third_party/blink/renderer/core/typed_arrays/dom_array_buffer.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
namespace base {
class SingleThreadTaskRunner;
}
namespace blink {
class Blob;
class DOMArrayBuffer;
class ExceptionState;
class ExecutionContext;
class FileReaderSync final : public ScriptWrappable {
DEFINE_WRAPPERTYPEINFO();
public:
static FileReaderSync* Create(ExecutionContext* context) {
return MakeGarbageCollected<FileReaderSync>(context);
}
explicit FileReaderSync(ExecutionContext*);
DOMArrayBuffer* readAsArrayBuffer(Blob*, ExceptionState&);
String readAsBinaryString(Blob*, ExceptionState&);
String readAsText(Blob* blob, ExceptionState& ec) {
return readAsText(blob, "", ec);
}
String readAsText(Blob*, const String& encoding, ExceptionState&);
String readAsDataURL(Blob*, ExceptionState&);
void Trace(Visitor* visitor) const override {
ScriptWrappable::Trace(visitor);
}
private:
absl::optional<FileReaderData> Load(const Blob&, ExceptionState&);
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_CORE_FILEAPI_FILE_READER_SYNC_H_