Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[zh-cn]: sync translation for File.{lastModified,lastModifiedDate,name} #20418

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 66 additions & 27 deletions files/zh-cn/web/api/file/lastmodified/index.md
Original file line number Diff line number Diff line change
@@ -1,61 +1,100 @@
---
title: File.lastModified
title: FilelastModified 属性
slug: Web/API/File/lastModified
l10n:
sourceCommit: 8fd2ee72038310e3ecc387df235ffac1cb08775c
---

{{APIRef("File API")}}
{{APIRef("File API")}}{{AvailableInWorkers}}

只读属性 **`File.lastModified`** 返回所引用文件最后修改日期,为自 1970 年 1 月 1 日 0:00 以来的毫秒数。没有已知的最后修改时间则会返回当前时间。

## 语法

```js
var time = instanceOfFile.lastModified;
```
{{domxref("File")}} 接口的 **`lastModified`** 只读属性提供文件的上次修改日期,作为自 Unix 纪元(1970 年 1 月 1 日午夜)以来的毫秒数。没有已知最后修改日期的文件返回当前日期。

## 值

自 1970 年 1 月 1 日 0:00 以来的毫秒数。
一个数字,表示自 Unix 纪元以来的毫秒数。

## 示例

## 实例
下面的示例将循环遍历你选择的文件,并打印每个文件在过去一年内是否被修改过。

### 从 INPUT 标签读取文件
### HTML

```html
<input type="file" multiple id="fileInput" />
<input type="file" id="filepicker" name="fileList" multiple />
<output id="output"></output>
```

```css hidden
output {
display: block;
white-space: pre-wrap;
}
```

### JavaScript

```js
const fileInput = document.getElementById("fileInput");
fileInput.addEventListener("change", function (event) {
// files is a FileList object (simliar to NodeList)
const files = event.target.files;
const output = document.getElementById("output");
const filepicker = document.getElementById("filepicker");

for (let i = 0; i < files.length; i++) {
const date = new Date(files[i].lastModified);
alert(files[i].name + " has a last modified date of " + date);
filepicker.addEventListener("change", (event) => {
const files = event.target.files;
const now = new Date();
output.textContent = "";

for (const file of files) {
const date = new Date(file.lastModified);
// 如果文件超过 1 年没有修改,则为 true
const stale = now.getTime() - file.lastModified > 31_536_000_000;
output.textContent += `${file.name} 是${
stale ? "陈旧的" : "新的"
}(${date})。\n`;
}
});
```

结果:
### 结果

{{ EmbedLiveSample('从 INPUT 标签读取文件', 300, 50) }}
{{EmbedLiveSample('示例')}}

### 动态创建文件

如果文件是动态创建的,可以在构造函数{{domxref("File.File()", "new File()")}} 中提供最后修改时间。如果未提供则会继承文件对象被创建时的{{jsxref("Date.now()")}}
如果文件是动态创建的,可以在 {{domxref("File.File()", "new File()")}} 构造函数中提供最后修改时间。如果未提供则会继承文件对象被创建时的 {{jsxref("Date.now()")}}。

```js
var fileWithDate = new File([], "file.bin", {
const fileWithDate = new File([], "file.bin", {
lastModified: new Date(2017, 1, 1),
});
console.log(fileWithDate.lastModified); //returns 1485903600000
console.log(fileWithDate.lastModified); // 返回 1485903600000

const fileWithoutDate = new File([], "file.bin");
console.log(fileWithoutDate.lastModified); // 返回当前时间
```

## 时间精度降低

var fileWithoutDate = new File([], "file.bin");
console.log(fileWithoutDate.lastModified); //returns current time
为了防止计时攻击和{{glossary("fingerprinting", "指纹识别")}},`someFile.lastModifiedDate.getTime()` 的精度可能会根据浏览器设置进行舍入。

在 Firefox 中,`privacy.reduceTimerPrecision` 首选项默认启用,在 Firefox 59 中默认为 20 微秒;Firefox 60 中为 2 毫秒。

```js
// Firefox 60 中的时间精度降低(2 毫秒)
someFile.lastModifiedDate.getTime();
// 1519211809934
// 1519211810362
// 1519211811670
// …

// 启用 `privacy.resistFingerprinting` 会降低时间精度
someFile.lastModifiedDate.getTime();
// 1519129853500
// 1519129858900
// 1519129864400
// …
```

在 Firefox 中,你还可以启用 `privacy.resistFingerprinting`,精度将为 100 毫秒 或 `privacy.resistFingerprinting.reduceTimerPrecision.microseconds` 的值,以较大者为准。

## 规范

{{Specifications}}
Expand Down
61 changes: 48 additions & 13 deletions files/zh-cn/web/api/file/lastmodifieddate/index.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,61 @@
---
title: File.lastModifiedDate
title: FilelastModifiedDate 属性
slug: Web/API/File/lastModifiedDate
l10n:
sourceCommit: 367b982b93c07f7f99e7bb768a6bf326fa5198e6
---

{{APIRef("File API")}}
{{APIRef("File API")}}{{AvailableInWorkers}}{{Deprecated_Header}}{{Non-standard_Header}}

## 概述
{{domxref("File")}} 接口的 **`lastModifiedDate`** 只读属性返回文件的最后修改日期。没有已知最后修改日期的文件则返回当前日期。

返回当前文件的最后修改日期,如果无法获取到文件的最后修改日期,则使用当前日期来替代。
## 值

一个 {{JSXRef("Global_Objects/Date", "Date")}} 对象,指示上次修改文件的日期和时间。

## 示例

```js
// fileInput 是一个 HTMLInputElement 元素:<input type="file" multiple id="myfileinput">
var fileInput = document.getElementById("myfileinput");

// files 是一个 FileList 对象 (类似于 NodeList 对象)
var files = fileInput.files;
// fileInput 是一个 HTMLInputElement:<input type="file" multiple id="myfileinput">
const fileInput = document.getElementById("myfileinput");

for (var i = 0; i < files.length; i++) {
alert(
files[i].name + " has a last modified date of " + files[i].lastModifiedDate,
);
for (const file of fileInput.files) {
console.log(`${file.name} 最后修改日期为 ${file.lastModifiedDate}`);
}
```

## 时间精度降低

为了防止计时攻击和{{glossary("fingerprinting", "指纹识别")}},`someFile.lastModifiedDate.getTime()` 的精度可能会根据浏览器设置进行舍入。

在 Firefox 中,`privacy.reduceTimerPrecision` 首选项默认启用,在 Firefox 59 中默认为 20 微秒;Firefox 60 中为 2 毫秒。

```js
// Firefox 60 中的时间精度降低(2 毫秒)
someFile.lastModifiedDate.getTime();
// 1519211809934
// 1519211810362
// 1519211811670
// …

// 启用 `privacy.resistFingerprinting` 会降低时间精度
someFile.lastModifiedDate.getTime();
// 1519129853500
// 1519129858900
// 1519129864400
// …
```

在 Firefox 中,你还可以启用 `privacy.resistFingerprinting`,精度将为 100ms 或 `privacy.resistFingerprinting.reduceTimerPrecision.microseconds` 的值,以较大者为准。
yin1999 marked this conversation as resolved.
Show resolved Hide resolved

## 规范

_尽管存在于文件 API 规范的早期草案中,但此属性已从中删除,并且此属性现在是非标准的。使用 {{domxref("File.lastModified")}} 代替。_

## 浏览器兼容性

{{Compat}}

## 参见

- {{domxref("File")}}
10 changes: 6 additions & 4 deletions files/zh-cn/web/api/file/name/index.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
---
title: File.name
title: Filename 属性
slug: Web/API/File/name
l10n:
sourceCommit: 8fd2ee72038310e3ecc387df235ffac1cb08775c
---

{{APIRef("File API")}}
{{APIRef("File API")}}{{AvailableInWorkers}}

返回由 {{domxref("File")}} 对象表示的文件的名称。由于安全原因,该属性并不包含文件路径。
{{domxref("File")}} 接口的 **`name`** 只读属性返回由 {{domxref("File")}} 对象表示的文件的名称。由于安全原因,该属性并不包含文件路径。

## 值

Expand Down Expand Up @@ -55,4 +57,4 @@ filepicker.addEventListener("change", (event) => {

## 参见

- [在 web 应用程序中使用文件](/zh-CN/docs/Web/API/File_API/Using_files_from_web_applications)
- [在 Web 应用程序中使用文件](/zh-CN/docs/Web/API/File_API/Using_files_from_web_applications)