From 218934fa2ed1c702a6d3923d2aa2cc6b43c48684 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:43:23 -0500 Subject: initial commit --- files/zh-tw/web/api/blob/blob/index.html | 125 +++++++++++++++++++++++++++++++ files/zh-tw/web/api/blob/index.html | 116 ++++++++++++++++++++++++++++ files/zh-tw/web/api/blob/size/index.html | 114 ++++++++++++++++++++++++++++ files/zh-tw/web/api/blob/type/index.html | 114 ++++++++++++++++++++++++++++ 4 files changed, 469 insertions(+) create mode 100644 files/zh-tw/web/api/blob/blob/index.html create mode 100644 files/zh-tw/web/api/blob/index.html create mode 100644 files/zh-tw/web/api/blob/size/index.html create mode 100644 files/zh-tw/web/api/blob/type/index.html (limited to 'files/zh-tw/web/api/blob') diff --git a/files/zh-tw/web/api/blob/blob/index.html b/files/zh-tw/web/api/blob/blob/index.html new file mode 100644 index 0000000000..26fb1e8ef7 --- /dev/null +++ b/files/zh-tw/web/api/blob/blob/index.html @@ -0,0 +1,125 @@ +--- +title: Blob() +slug: Web/API/Blob/Blob +translation_of: Web/API/Blob/Blob +--- +

{{APIRef("File API")}}

+ +

Blob() 建構式會回傳一個新建立的 {{domxref("Blob")}} 物件。新物件的內容是由 array 參數的成員值串連所構成。

+ +

語法

+ +
var aBlob = new Blob( array, options );
+
+ +

參數

+ + + +

範例

+ +
var aFileParts = ['<a id="a"><b id="b">hey!</b></a>']; // an array consisting of a single DOMString
+var oMyBlob = new Blob(aFileParts, {type : 'text/html'}); // the blob
+ +

規範

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('File API', '#constructorBlob', 'Blob()')}}{{Spec2('File API')}}Initial definition.
+ +

瀏覽器相容性

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
功能ChromeFirefox (Gecko)Internet ExplorerOperaSafari
+

基礎支援

+
20{{CompatGeckoDesktop("13.0")}} [1]1012.108
在 Workers 中{{CompatUnknown}}{{CompatGeckoDesktop("14.0")}}{{CompatUnknown}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
功能AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
基礎支援{{CompatUnknown}}{{CompatGeckoMobile("13.0")}} [1]{{CompatNo}}{{CompatUnknown}}{{CompatUnknown}}
在 Workers 中{{CompatUnknown}}{{CompatGeckoMobile("14.0")}}{{CompatNo}}{{CompatUnknown}}{{CompatUnknown}}
+
+ +

[1] 在 Firefox 16 以前,第二個參數若被設為 nullundefined 會導致錯誤而不是當成一個空鍵值。

+ +

參見

+ + + +

 

diff --git a/files/zh-tw/web/api/blob/index.html b/files/zh-tw/web/api/blob/index.html new file mode 100644 index 0000000000..457628556c --- /dev/null +++ b/files/zh-tw/web/api/blob/index.html @@ -0,0 +1,116 @@ +--- +title: Blob +slug: Web/API/Blob +translation_of: Web/API/Blob +--- +

{{APIRef("File API")}}

+ +

Blob(Binary Large Object)物件代表了一個相當於檔案(原始資料)的不可變物件。Blob 中的資料並不一定是 JavaScript 原生的格式。{{domxref("File")}} 介面基於 Blob,繼承 blob 並擴充其功能以支援操作使用者系統上的檔案。

+ +

從其它非 Blob 物件或資料來建構 Blob 物件,可以使用 {{domxref("Blob.Blob", "Blob()")}} 建構式。要建立一個包含目前 blob 內容子集的 blob,可使用 {{domxref("Blob.slice()", "slice()")}} 方法。若要自使用者系統上的檔案取得 Blob 物件,請參考 {{domxref("File")}} 文件。

+ +

接受 Blob 物件的 API 可以在 {{domxref("File")}} 上找到。

+ +
+

註:早期 slice() 方法擁有第二個參數 length 以指定在建立新 Blob 物件時要複製的位元組(byte)數量。假如指定的 start + length 超出了來源 Blob 的大小,則回傳的 Blob 會包含自索引 start 至結尾的完整來源內容。

+
+ +
+

註:需注意在部分瀏覽器版本中,slice() 方法帶有前綴:Firefox 12 與之前的版本為 blob.mozSlice(),Safari 中是 blob.webkitSlice()。舊的、無前綴字版本的 slice() 方法則有不同的語意(semantics),但這是已淘汰的方法。瀏覽器對 blob.mozSlice() 的支援已在 Firefox 30 時中止。

+
+ +

建構式

+ +
+
{{domxref("Blob.Blob", "Blob(blobParts[, options])")}}
+
回傳新建立的 Blob 物件,包含了建構式參數傳入之陣列所串聯後的值。第二個參數為 BlobPropertyBag 物件,其擁有 type 和 endings 屬性
+
+ +

屬性

+ +
+
{{domxref("Blob.size")}} {{readonlyinline}}
+
以 byte 為單位的 Blob 物件大小。
+
{{domxref("Blob.type")}} {{readonlyinline}}
+
Blob 物件中資料的型態,以 MIME 類型的字串表示。若型態為未知,則為空字串。
+
+ +

方法

+ +
+
{{domxref("Blob.slice()", "Blob.slice([start[, end[, contentType]]])")}}
+
回傳一個包含當前 Blob 物件之指定資料範圍(byte)內容的新 Blob 物件。
+
+ +

範例

+ +

Blob 建構函數用法範例

+ +

{{domxref("Blob.Blob", "Blob() constructor")}} 建構式允許由其它物件建立 blob 物件。以下的範例演示了以字串來建構 blob 物件:

+ +
var debug = {hello: "world"};
+var blob = new Blob([JSON.stringify(debug, null, 2)], {type : 'application/json'});
+ +
+

在 Blob 建構式出現之前,可以透過 {{domxref("BlobBuilder")}} 來建立 blob 物件(目前已不建議使用):

+ +
var builder = new BlobBuilder();
+var fileParts = ['<a id="a"><b id="b">hey!</b></a>'];
+builder.append(fileParts[0]);
+var myBlob = builder.getBlob('text/xml');
+
+ +

藉型別陣列建構的 blob 來建立 URL

+ +

範例程式碼:

+ +
var typedArray = GetTheTypedArraySomehow();
+var blob = new Blob([typedArray], {type: 'application/octet-binary'}); // pass a useful mime type here
+var url = URL.createObjectURL(blob);
+// url will be something like: blob:d3958f5c-0777-0845-9dcf-2cb28783acaf
+// now you can use the url in any context that regular URLs can be used in, for example img.src, etc.
+
+ +

從 Blob 取出資料

+ +

從 Blob 讀取資料的唯一方式就是使用 {{domxref("FileReader")}}。以下範例展示了讀取 Blob 內容作為型別陣列:

+ +
var reader = new FileReader();
+reader.addEventListener("loadend", function() {
+   // reader.result contains the contents of blob as a typed array
+});
+reader.readAsArrayBuffer(blob);
+ +

藉由操作 {{domxref("FileReader")}} 的其他方法,將 Blob 讀取成字串或是 data URL 是有可能的。

+ +

規範

+ + + + + + + + + + + + + + +
規範狀態註解
{{SpecName('File API','#blob','Blob')}}{{Spec2('File API')}}Initial definition.
+ +

瀏覽器相容性

+ + + +

{{Compat("api.Blob")}}

+ +

參見

+ + diff --git a/files/zh-tw/web/api/blob/size/index.html b/files/zh-tw/web/api/blob/size/index.html new file mode 100644 index 0000000000..0abccad27d --- /dev/null +++ b/files/zh-tw/web/api/blob/size/index.html @@ -0,0 +1,114 @@ +--- +title: Blob.size +slug: Web/API/Blob/size +tags: + - API + - Files + - Property + - Reference + - WebAPI + - 參考 + - 屬性 +translation_of: Web/API/Blob/size +--- +
{{APIRef("File API")}}
+ +

Blob.size 屬性回傳以 byte 為單位的 {{domxref("Blob")}} 或一個 {{domxref("File")}} 的大小。

+ +

語法

+ +
var sizeInBytes = blob.size
+
+ +

+ +

一個數字。

+ +

範例

+ +
// fileInput 是個 HTMLInputElement: <input type="file" multiple id="myfileinput">
+var fileInput = document.getElementById("myfileinput");
+
+// files 是個 FileList 物件 (類似 NodeList)
+var files = fileInput.files;
+
+for (var i = 0; i < files.length; i++) {
+  console.log(files[i].name + " has a size of " + files[i].size + " Bytes");
+}
+ +

規格

+ + + + + + + + + + + + + + +
規格狀態附註
{{SpecName('File API', '#dfn-size', 'size')}}{{Spec2('File API')}}初定義
+ +

瀏覽器相容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + +
功能ChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
File.size5{{CompatVersionUnknown}}{{CompatGeckoDesktop("2")}}10.011.105.1
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
功能AndroidEdgeFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
File.size{{CompatNo}}{{CompatVersionUnknown}}{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatNo}}
+
+ +

參見

+ + diff --git a/files/zh-tw/web/api/blob/type/index.html b/files/zh-tw/web/api/blob/type/index.html new file mode 100644 index 0000000000..ee2a231724 --- /dev/null +++ b/files/zh-tw/web/api/blob/type/index.html @@ -0,0 +1,114 @@ +--- +title: Blob.type +slug: Web/API/Blob/type +translation_of: Web/API/Blob/type +--- +
{{APIRef("File API")}}
+ +

Blob 物件的 type 屬性提供檔案的 MIME 類別。若無法辨明型別則回傳空字串。

+ +

語法

+ +
var mimetype = instanceOfFile.type
+ +

+ +

一個字串。

+ +

範例

+ +
var i, fileInput, files, allowedFileTypes;
+
+// fileInput 是個 HTMLInputElement: <input type="file" multiple id="myfileinput">
+fileInput = document.getElementById("myfileinput");
+
+// files 是個 FileList 物件 (類似 NodeList)
+files = fileInput.files;
+
+// 這範例接受 *.png, *.jpeg 和 *.gif 圖片。
+allowedFileTypes = ["image/png", "image/jpeg", "image/gif"];
+
+for (i = 0; i < files.length; i++) {
+  // 測試 file.type 是否是允許的類別。
+  if (allowedFileTypes.indexOf(files[i].type) > -1) {
+    // 若符合則執行這裡的程式碼。
+  }
+});
+
+ +

規格

+ + + + + + + + + + + + + + +
規格狀態備註
{{SpecName('File API', '#dfn-type', 'type')}}{{Spec2('File API')}}初定義
+ +

瀏覽器相容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + +
功能ChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
File.type5{{CompatVersionUnknown}}{{CompatGeckoDesktop("2")}}10.011.105.1
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
功能AndroidEdgeFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
File.type{{CompatNo}}{{CompatVersionUnknown}}{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatNo}}
+
+ +

參見

+ + -- cgit v1.2.3-54-g00ecf