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

FileList

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

FileList 接口表示由 HTML <input> 元素的 files 属性返回的该类型的对象;这使你可以访问使用 <input type="file"> 元素选择的文件列表。它还用于使用拖放 API 中放入 Web 内容中的文件列表;有关此用法的详细信息,请参阅 DataTransfer 对象。

所有 <input> 元素节点都有一个 FileList 类型的 files 属性,其允许访问此列表中的项目。例如,如果 HTML 包含以下文件输入框:

htmlCopy to Clipboard

<input id="fileItem" type="file" />

以下代码行将节点文件列表中的第一个文件作为 File 对象获取:

jsCopy to Clipboard

const file = document.getElementById("fileItem").files[0];

此接口试图创建不可修改的列表,为了不破坏已经使用该接口的代码,才继续支持该接口。现代 API 使用基于 JavaScript 数组的类型来表示列表结构,从而提供许多数组方法,同时对其使用施加额外的语义(例如使其项目为只读)。

这些历史原因并不意味着你作为开发人员应该避免使用 FileList。你不会自己创建 FileList 对象,而是从诸如 HTMLInputElement.files 之类的 API 获取它们,并且这些 API 并未弃用。但是,请注意与真实数组的语义差异。

实例属性

length 只读

指示列表中文件数量的只读值。

实例方法

item()

返回一个 File 对象,表示文件列表中指定索引处的文件。

1、FileList接口在c++定义

 third_party\blink\renderer\core\fileapi\file_list.idl

/*
 * Copyright (C) 2008 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:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. 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.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``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 APPLE INC. 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/#filelist-section

[
    Exposed=(Window,Worker),
    Serializable
] interface FileList {
    getter File? item(unsigned long index);
    readonly attribute unsigned long length;
};

2、FileList接口c++实现

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

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

third_party\blink\renderer\platform\bindings\script_wrappable.h

/*
 * Copyright (C) 2008 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:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. 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.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``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 APPLE INC. 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_LIST_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_FILEAPI_FILE_LIST_H_

#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/fileapi/file.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/vector.h"

namespace base {
class FilePath;
}

namespace blink {

class CORE_EXPORT FileList final : public ScriptWrappable {
  DEFINE_WRAPPERTYPEINFO();

 public:
  FileList();

  unsigned length() const { return files_.size(); }
  File* item(unsigned index) const;

  bool IsEmpty() const { return files_.empty(); }
  void clear() { files_.clear(); }
  void Append(File* file) { files_.push_back(file); }
  Vector<base::FilePath> PathsForUserVisibleFiles() const;

  void Trace(Visitor*) const override;

 private:
  HeapVector<Member<File>> files_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_FILEAPI_FILE_LIST_H_

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值